I have a DropDownList for year selection and I filled it with following default values.
Instead of which, is there any option to fill it using for-loop like I have given in below code as comment.
var yr = new List<Object>
{
new { value=0 ,text="--Select--"},
new { value = 2015 , text = "2015" },
new { value = 2016 , text = "2016" },
new { value = 2017 , text = "2017" },
new { value = 2018 , text = "2018" },
new { value = 2019 , text = "2019" },
new { value = 2020 , text = "2020" },
//new { for(int i = 2010;i <= DateTime.Now.Year+5; i++) value = i , text = i },
};
ViewBag.Year = new SelectList(yr, "value", "text");
List<T>has an "Add" method...SelectListItemnotobjectand just assign that toViewBag.Year- there is no need to theSelectListconstructor. And add thelabelOptionin theDropDownListFor()method, not in the controller.Enumerable.Range()methodViewBag.Year = Enumerable.Range(2010, DateTime.Now.Year + 5 - 2010).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });