Convert my list <int> to list <SelectListItem>
I have a DropDownList containing a range of ten years (from 2010 to 2020) created as follows:
var YearList = new List<int>(Enumerable.Range(DateTime.Now.Year - 5, ((DateTime.Now.Year + 3) - 2008) + 1)); ViewBag.YearList = YearList; But here is my problem, I want to select the default value and save this value when I send my information, and I want to use the List<SelectListItem> type for it, since it is more practical.
As soon as in this type I will just do so to save the selected value:
foreach (SelectListItem item in list) if (item.Value == str) item.Selected = true; How to convert my List<int> to List<SelectListItem> ?
+6
user5448913
source share2 answers