I have two multi-select list boxes; one with months and one with years such as the following:
@Html.ListBoxFor(m => m.IncludeGuestsArrivedInTimeframeMonths, Model.TimeframeMonths)
@Html.ListBoxFor(m => m.IncludeGuestsArrivedInTimeframeYears, Model.TimeframeYears)
I'm saving the both values to the database as a comma separated list (string/nvarchar). The values are being like this: Months: 1,3,7; Years: 2002,2005
For some reason when I pull the values back out to the form, the months are pre-selecting in the listbox fine, but the years are not.
Any ideas?
EDIT - Additional code samples:
Controller - Manage
public ActionResult Manage(Guid id)
{
var list = _listService.GetList(id);
var model = LoadModelFromObject(list);
model.TimeframeMonths = GenerateMonthDropdown();
model.TimeframeYears = GenerateYearDropdown(model.IncludeGuestsArrivedInTimeframeYears.Split(','));
model.DaysOfWeek = GenerateDaysOfWeekDropdown();
return View(model);
}
Controller - 'Helper'
private IList<SelectListItem> GenerateYearDropdown(string[] selected)
{
var list = new List<SelectListItem>();
var startYear = DateTime.Now.Year - 10;
for (int idx = startYear; idx < startYear + 11; idx++)
{
list.Add(new SelectListItem
{
Value = idx.ToString(),
Text = idx.ToString(),
Selected = selected != null && selected.Contains(idx.ToString())
});
}
return list;
}
View
@Html.CheckBoxFor(m => m.IncludeGuestsArrivedInTimeframe)
@Html.LabelFor(m => m.IncludeGuestsArrivedInTimeframe)
@Html.ListBoxFor(m => m.IncludeGuestsArrivedInTimeframeMonths, Model.TimeframeMonths)
@*@Html.ListBoxFor(m => m.IncludeGuestsArrivedInTimeframeYears, Model.TimeframeYears)*@
@Html.ListBox("IncludeGuestsArrivedInTimeframeYears", Model.TimeframeYears)
Model.TimeframeYearsreturn? I mean the values. It must return type ofIEnumerable<SelectListItem>but I cannot see what values it has.IList<SelectListItem>and the Value and Text of each SelectListItem are the year (2002)IncludeGuestsArrivedInTimeframeYearsandTimeframeYearsproperties?