In my controller I set the SelectedYear field as follows; [Authorize(Roles = "Administrator, AdminAccounts")] public ActionResult BankHoliday(int? id) { BankHolidayViewModel BankHolidayViewModel = new BankHolidayViewModel();
int year = DateTime.Now.Year;
if (id.HasValue)
year = id.GetValueOrDefault();
BankHolidayViewModel.SelectedYear = year;
BankHolidayViewModel.GetYearData(year);
return View(BankHolidayViewModel);
}
Whenever I step through the code, the SelectedYear value is always correct on the return statement. In the Year DropDownList below, this is the Value field that sets the selected item in the DropDownList;
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage" %>
BankHoliday
Bank Holiday Administration
Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%>
$(function () { $("#SelectedYear").change(function () { var year = $("#SelectedYear").val(); $("#wholepage").load("/AdminAccounts/BankHoliday/" + year); }); });* EDIT: ADDED INFO *
The YearList is defined as follows; public class BankHolidayViewModel { public IList BankHolidays { get; set; } public IList Years { get; set; } public int SelectedYear = DateTime.Now.Year; public IEnumerable YearList { get { return this.Years.Select(item => new SelectListItem { Text = item.Year.ToString(), Value = item.Year.ToString() }); } }
* END EDIT *
For some reason when the View is rendered, there is no selected item in the dropdownlist. How do I fix this?