I have a DropDownList
containing a range of ten years (from 2010 to 2020) created like such :
var YearList = new List<int>(Enumerable.Range(DateTime.Now.Year - 5, ((DateTime.Now.Year + 3) - 2008) + 1));
ViewBag.YearList = YearList;
But here's my problem, I wish to have a default value selected and keep this value when I submit my information and I wish to use the type List<SelectListItem>
for it, since its more practical.
Once in this type I will simply do as such to keep a selected value:
foreach (SelectListItem item in list)
if (item.Value == str)
item.Selected = true;
How may I convert my List<int>
into a List<SelectListItem>
?
item.Selected = true
! You bind to a model property. See my comments to your last question