12
我有一個像SelectedValues在MultiSelectList不工作MVC
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;
}
}
在控制器I類填充MiltiselectItems和設置selectedValues它
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);
}
在查看代碼我有
@Html.ListBox("Category", Model.Categories)
運行我的操作時SelectedValues不起作用。我做錯了什麼?