1 //Product 实体 
 2 public class Product
 3     {
 4         [HiddenInput(DisplayValue = false)]
 5         public int ProductID { getset; }
 6         [Required(ErrorMessage = "Please enter a product name")]
 7         public string Name { getset; }
 8         [Required(ErrorMessage = "Please enter a description")]
 9         [DataType(DataType.MultilineText)]
10         public string Description { getset; }
11         [Required]
12         [Range(0.01double.MaxValue, ErrorMessage = "Please enter a positive price")]
13         public decimal Price { getset; }
14         [Required(ErrorMessage = "Please specify a category")]
15         public string Category { getset; }
16         public byte[] ImageData { getset; }
17         [HiddenInput(DisplayValue = false)]
18         public string ImageMimeType { getset; }
19 
20     }
//View

@model List<List<Product>>
//----------------不用理会-----------------
@{
    ViewBag.Title = "Admin: All Products";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    
}
<h1>
    All Products</h1>
<table class="Grid">
    <tr>
        <th>
            ID
        </th>
        <th>
            Name
        </th>
        <th class="NumericCol">
            Price
        </th>
        <th>
            Actions
        </th>
    </tr>
//----------------不用理会-----------------
//----------------重点---------------------
//必须使用for循环来做模型绑定,MVC内置使用用[]索引来绑定集合的
    @using (Html.BeginForm("Test", "Admin"))//ActionName,ControllerName
    {
        for (int i = 0; i < Model.Count; i++)
        {
            for (int j 
= 0; j < Model[i].Count; j++)
            {
        <tr
>
            <td>
            //m 是什么?你懂的
            @Html.LabelFor(m => m[i][j].ProductID)
            @Html.HiddenFor(m => m[i][j].ProductID)
            @Html.HiddenFor(m => m[i][j].Name)
        </tr>
          }
      }
        <input type="submit" value="Test" />
  }
//Controller:
//加断点即可查看传进来的参数,并行自定义对象集合都可以传,更别说单集合对象了(List<Product>) 自己改造下即可,你懂的
      public ViewResult Test(List<List<Product>> list)
        {
            return View();
        }

相关文章: