I am trying to work with a generated Month dropdownlist in MVC. My viewmodel is:
public class MyViewModel{
public MyViewModel()
{
var monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
Months = new SelectList(monthNames.Select(m=> new{Id=monthNames.IndexOf(m)+1, Name=m}).ToList(),"Id","Name");
}
public IEnumerable<SelectListItem> Months{ get; set; }
public string Month{ get; set; }
}
My View is:
@Html.DropDownListFor(model=>model.Month, new SelectList(Model.Months))
The problem is that the Months property always returns a null value so the page errors when trying to render the DDL.
Seems pretty simple. What am I missing?
MonthNamesactually returns a fixed String[13], so I'm assuming it was OPs way of removing the last empty element (though, IMHO, that kind of defeats the whole localization point). ;p