MVC模型绑定的IList< T>数据Html.Listbox [英] MVC Model Binding IList<T> data to Html.Listbox

查看:552
本文介绍了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的文章:
<一href=\"http://www.altafkhatri.com/Technical/How%5Fto%5Fbind%5FIList%5Fwith%5FMVC%5FDropdownlist%5Fbox\" rel=\"nofollow\">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

这篇关于MVC模型绑定的IList&LT; T&GT;数据Html.Listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆