Hi, I want to bind my Store class which has multiple Products to Html.Listbox. While in edit Store mode, I want Html.Listbox show all products where products of the Store are selected. I could not manage to bind store.Products to the listbox
My class structure;
public class Store
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class StoreEditView
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual MultiSelectList ProductList //populated from db, all products in the form of IList<Product>
}
My controller;
public ViewResult Edit()
{
var editstore = new StoreEditView();
editstore.Products = new List<Product> {new Product() {Id = 1, Name="Example"}};
return View(editstore);
}
My View;
<%=Html.ListBox("Products", Model.ProductList)%>
In this case, I need product.Id=1 to be shown selected in the listbox. So far I couldn't do it. I tried,
<%=Html.ListBox("Product.Id", Model.ProductList)%>
<%=Html.ListBox("Products.Id", Model.ProductList)%>
just didn't work.