• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
袜子不回头
一个人走着走着摔倒了,爬起来,又摔倒了,又爬起来,又摔倒了,死循环了……
博客园    首页    新随笔    联系   管理    订阅  订阅

Asp.net MVC View 向Control传递集合

 1 //Product 实体 
 2 public class Product
 3     {
 4         [HiddenInput(DisplayValue = false)]
 5         public int ProductID { get; set; }
 6         [Required(ErrorMessage = "Please enter a product name")]
 7         public string Name { get; set; }
 8         [Required(ErrorMessage = "Please enter a description")]
 9         [DataType(DataType.MultilineText)]
10         public string Description { get; set; }
11         [Required]
12         [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
13         public decimal Price { get; set; }
14         [Required(ErrorMessage = "Please specify a category")]
15         public string Category { get; set; }
16         public byte[] ImageData { get; set; }
17         [HiddenInput(DisplayValue = false)]
18         public string ImageMimeType { get; set; }
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();
        }
posted @ 2011-10-17 16:33  袜子不回头  阅读(3228)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3