Im trying to create a SelectList for Dates, in format MMMM/yyyy
, in the TEXT property and yyyy-MM-dd
in the VALUE property.
Everything works good, but when I use this SelectList in a edit option
and I have to fill the form to edit, the SELECTED property doesn't work and I don't know why...
My method to create the list is:
public SelectList GetSLDate(DateTime _dateLimit, DateTime? _selected = null)
{
var list = new List<SelectItemList>();
var date = new DateTime(2015, 1, 1);
int month = Math.Abs(12 * (_dateLimit.Year - date.Year) + (_dateLimit.Month - date.Month + 1));
for (int m = (month - 1); m >= 0; m--)
{
var dateRef = date.AddMonths(m);
list.Add(
new SelectListItem
{
Text = dateRef.ToString("MMMM/yyyy"),
Value = dateRef.ToString("yyyy-MM-dd")
}
);
return new SelectList(list, "Value", "Text", ((_selected == null) ? null : (((DateTime)_selected).ToString("yyyy-MM-dd"))));
}
}
When you create your SelectListItem, try setting the
Selected
property: