barzin.A barzin.A - 29 days ago 7
ASP.NET (C#) Question

Create list for dropdownlist in asp.net MVC

i'm using this ViewModel

public class SliderViewModel
{
public Models.Slider Slider { get; set; }
public System.Collections.Generic.IList<System.Web.Mvc.SelectListItem> Priorities { get; set; }
public int Selected_Priority { get; set; }
}


now i want to create list to bind to dropdownlist in view

IList<System.Web.Mvc.SelectListItem> list = new IList<System.Web.Mvc.SelectListItem>();

for (int i = 1; i <= SliderCount; i++)
{
list.Add(new SelectListItem() { Value = i.ToString(), Text = i.ToString() });
}

objSlider.Priorities = list;


i'm using this code but get an error

Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList'

Answer

try:

IList<System.Web.Mvc.SelectListItem> list = new List<System.Web.Mvc.SelectListItem>();

notice the right-hand said is type List.

You cannot directly instantiate an interface, that's a rule of the framework.