MVC模型绑定的IList< T>数据Html.Listbox

MVC模型绑定的IList< T>数据Html.Listbox

问题描述:

我要绑定有多个产品才能Html.Listbox我的商店类。
在编辑存储模式,我想Html.Listbox显示那里的商店,产品选择的所有产品。我不能设法store.Products绑定到ListBox

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

我的阶级结构;

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>
}

我的控制器;

  public ViewResult Edit()
  {
    var editstore = new StoreEditView();
    editstore.Products = new List<Product> {new Product() {Id = 1, Name="Example"}};
    return View(editstore);
  }

我的视图;

 <%=Html.ListBox("Products", Model.ProductList)%>

在这种情况下,我需要product.Id = 1要显示在列表框中选择。到目前为止,我不能这样做。
我试过了,

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)%>

只是没有工作。

请参阅介绍如何绑定列表框MVC的文章:
http://www.altafkhatri.com/Technical/How%5Fto%5Fbind%5FIList%5Fwith%5FMVC%5FDropdownlist%5Fbox

Please refer the article which describes how to bind MVC listbox: http://www.altafkhatri.com/Technical/How%5Fto%5Fbind%5FIList%5Fwith%5FMVC%5FDropdownlist%5Fbox