I have a class like
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Category> CategorySelected { get; set; }
public static List<Category> GetOptions()
{
var categories = new List<Category>();
categories.Add(new Category() { ID = 1, Name = "Bikes" });
categories.Add(new Category() { ID = 2, Name = "Cars" });
categories.Add(new Category() { ID = 3, Name = "Trucks" });
return categories;
}
}
In the controller I Fill MiltiselectItems and set selectedValues for it
public ActionResult Index()
{
Category cat=new Category();
cat.CategorySelected.Add(new Category { ID =1, Name = "Bikes" });
cat.CategorySelected.Add(new Category { ID =3, Name = "Trucks" });
var list = Category.GetOptions();
product.Categories = new MultiSelectList(list, "ID", "Name", CategorySelected);
}
In View Code I have
@Html.ListBox("Category", Model.Categories)
when run my action SelectedValues aren't working. What I'm doing wrong ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…