tags:

views:

25

answers:

2

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?

+1  A: 

Your "Inherits" needs to inherit your ViewModel.

Inherits="System.Web.Mvc.ViewPage<ViewModels.BankHolidayViewModel>"

When you create the YearList, you also need to set the selected value. Something like

 YearList = new SelectList(GetYears(), "year", "year", SelectedYear);

EDIT In your view you can set the selected value:

<%= Html.DropDownListFor(x => x.SelectedYear, new SelectList(Model.YearList, "Value", "Text", Model.SelectedYear))%>

Or you can do it when you originally set up the Year List as I put above. GetYears() would return the years for the list.

public SelectList YearList { get; private set; }
public class BankHolidayViewModel(int year)
{
    YearList = new SelectList(GetYears(), "Value", "Text", year);

Then when you return the view

return View(new BankHolidayViewModel(year));
RememberME
It does, but somehow this got truncated when this got published. So my inherits clause looks like; Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>"
arame3333
Edited answer..
RememberME
A: 

I didn't really want to change my object to a SelectList, although I am sure that would have worked. Instead I got the answer by changing the getter;

public IEnumerable<SelectListItem> YearList
{
    get
    {
        return this.Years.Select(item => new SelectListItem
        {
            Text = item.Year.ToString(),
            Value = item.Year.ToString(),
            Selected = item.Year == this.SelectedYear
        });
    }
}
arame3333