asp.net-mvc-5 – ASP.NET MVC中Controller的模型对象列表的列表
形式如下:
https://gyazo.com/289a1ac6b7ecd212fe79eec7c0634574
视图模型: public class ProductViewModel { public string Product { get; set; } public IEnumerable<SizeColorQuantityViewModel> SizeColorQuantities { get; set; } } public class SizeColorQuantityViewModel { public string ColorId { get; set; } public List<SizeAndQuantity> SizeAndQuantities { get; set; } } public class SizeAndQuantity { public int SizeId { get; set; } public int Quantity { get; set; } } 视图: @model ProjectSem3.Areas.Admin.Models.ProductViewModel @{ ViewBag.Title = "Create"; Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml"; string[] ListColor = { "Red","Blue" }; string[] ListSize = { "S","M","L","XL" }; } @for (var i = 0; i < ListColor.Length; i++) { <div class="form-group"> <label class="col-md-2 control-label">Color:</label> <div class="col-md-2"> @Html.TextBox("[" + i + "].ColorId",null,new { @Value = ListColor[i],@class = "form-control",@readonly = "readonly" }) </div> </div> <div class="form-group"> <label class="col-md-2 control-label">Size and Quantity:</label> @for (var j = 0; j < ListSize.Length; j++) { <div class="col-md-2"> @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.SizeId",new { @class = "form-control",@style = "margin-bottom: 15px",@Value = ListSize[j],@readonly = "readonly" }) @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.Quantity",new { @class = "form-control" }) </div> } </div> } 控制器: // GET: Admin/Product public ActionResult Create() { return View(); } // POST: Admin/Product [HttpPost] public ActionResult Create(ProductViewModel product,IEnumerable <SizeColorQuantityViewModel> sizeColorQuantity,IEnumerable <SizeAndQuantity> sizeAndQuantity) { return View(); } 我可以获得从ViewModel IEnumerable< SizeColorQuantityViewModel>传递的值. sizeColorQuantity to Controller.但是使用这个模型IEnumerable< SizeAndQuantity> sizeAndQuantity,我无法获得任何价值. 解决方法
生成的输入具有与模型无关的名称属性,因此不能由DefaultModelBinder绑定.您需要首先在控制器中生成数据(而不是在视图中),以便绑定到模型.
以下假设您将SizeColorQuantities属性更改为List< SizeColorQuantityViewModel> public ActionResult Create() { var colors = new List<string>(){ "Red","Blue" }; var sizes = new List<string>(){ "S","XL" }; var model = new ProductViewModel() { Product = "My product",SizeColorQuantities = new List<SizeColorQuantityViewModel> }; foreach(var color in colors) { var child = new SizeColorQuantityViewModel() { ColorId = color,SizeAndQuantities = new List<SizeAndQuantity> }; model.SizeColorQuantities.Add(child); foreach(var size in sizes) { child.SizeAndQuantities.Add(new SizeAndQuantity() { SizeId = size // assumes SizeId is changed to string,not int }); } } return View(model); } 您现在拥有一个正确填充的视图模型,该模型将传递给视图,您可以使用嵌套for循环中的强类型HtmlHelper将其绑定到该视图 @model ProductViewModel .... @using (Html.BeginForm()) { .... @for(int i = 0; i < Model.SizeColorQuantities.Count; i++) { @Html.TextBoxFor(m => m.SizeColorQuantities[i].ColorId,new { @class = "form-control",@readonly = "readonly" }) for (int j = 0; j < Model.SizeColorQuantities[i].SizeAndQuantities .Count; j++) { @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].SizeId,@readonly = "readonly" }) @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].Quantity,new { @class = "form-control" }) } } <input type="submit" ... /> } 注意:始终使用强类型*** For()HtmlHelper方法,并且在使用HtmlHelper方法时永远不要尝试设置值(或名称)属性. 你的POST方法现在需要 [HttpPost] public ActionResult Create(ProductViewModel model) { .... } 请注意,您还可以使用自定义EditorTemplate作为类型,如HTML Table to ADO.NET DataTable中所述,这也解释了如何生成名称属性以绑定到集合.在您的情况下,例如您的数量输入需要(与您当前生成的数量相比) <input name="SizeColorQuantities[0].SizeAndQuantities[0].Quantity" ... /> <input name="SizeColorQuantities[0].SizeAndQuantities[1].Quantity" ... /> .... <input name="SizeColorQuantities[1].SizeAndQuantities[0].Quantity" ... /> <input name="SizeColorQuantities[1].SizeAndQuantities[1].Quantity" ... /> .... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 优化性能或属性路由和路由表
- asp.net-mvc – 使用存储库模式来支持多个提供程序
- Azure 上部署 ASP.NET Core Web App
- asp.net-mvc – 存储库模式中的纯POCO实体更新问题
- asp.net – 从DropDownList中删除列表项
- asp.net – 从请求中获取所有发布数据
- 身份验证 – 如何为Asp.NET MVC 5创建ClaimsIdentity对象?
- asp.net – 我可以确定HttpModules是按照HttpApplication.M
- asp.net-mvc – Asp.net MVC动态菜单系统
- asp.net-mvc – 我在哪里可以找到nuPack上列出的MvcScaffol
- asp.net – 如何向IIS添加反射权限或将其添加到w
- asp.net-web-api – Web API中的OData POST的媒体
- asp.net-mvc-4 – 如何实现自定义SiteMapNodePro
- asp.net – 模型绑定与禁用文本框
- asp.net – 获取连接到SignalR hub的客户端数量
- asp.net-mvc – 使用ValueInjecter在具有不同属性
- 使用ASP.Net MVC3中的jQuery动态填充下拉列表
- asp.net-mvc-4 – 带有附加属性的ScriptBundle
- 如何在ASP.Net webform中使用标签?
- asp.net-mvc-3 – 为什么这个代码不删除布局?