需要将List 传递给Controller中的Http Post

我正在将一个List传递给视图。 在视图中,我需要传回该列表。 它包括一个可编辑的复选框,这是我真正需要的项目。 显示的所有其他字段仅供读取使用。 如果用户想要将项目指定为许可,则他们会检查isClearance复选框。 因此,当他们点击保存按钮时,它会点击HttpPost索引()。 但是,它会传回null。 如果我将它改为仅仅是一个modelObject而不是List,它就可以正常工作。

这是控制器索引:

public ActionResult Index() { List cc = new List(); ClearanceViewModel c = new ClearanceViewModel(); c.sku = "123"; c.title = "Test1"; c.includeOnSite = true; c.productID = 123; c.salePrice = Convert.ToDecimal(2.99); c.RetailPrice = Convert.ToDecimal(4.99); c.isClearance = false; cc.Add(c); c.sku = "123"; c.title = "Test1"; c.includeOnSite = true; c.productID = 123; c.salePrice = Convert.ToDecimal(2.99); c.RetailPrice = Convert.ToDecimal(4.99); c.isClearance = false; cc.Add(c); return View(cc); } 

这是视图:

 @model List @{ ViewBag.title = "Clearance List"; } 
Clearance List @using (Html.BeginForm()) { if (Model.Count() > 0) { @foreach(var item in Model) { @Html.HiddenFor(modelItem => item.productID); }
Sku Title Include On Site Sale price Retail Price Quantity Clearance
@Html.DisplayFor(modelItem => item.sku) @Html.DisplayFor(modelItem => item.title) @Html.DisplayFor(modelItem => item.includeOnSite) @Html.DisplayFor(modelItem => item.salePrice) @Html.DisplayFor(modelItem => item.RetailPrice) @Html.DisplayFor(modelItem => item.quantity) @Html.EditorFor(modelItem => item.isClearance)

} }

这是Controller中的HttpPost(当它遇到foreach循环时,clearanceModel为null,当它应该有一个项目列表时):

  [HttpPost] public ActionResult Index(List clearanceModel) { foreach (ClearanceViewModel item in clearanceModel) { if (item.isClearance == true) { // get the product object, so we can add it to the product Promotion of clearance var product = _unitOfWork.ProductRepository.Get(filter: p => p.productID == item.productID).FirstOrDefault(); // Make sure that it isn't already in the product promotion for clearance var productPromotion = _unitOfWork.ProductPromotionRepository.Get(filter: pp => pp.productID == product.productID && pp.promotion.Name == "Clearance").FirstOrDefault(); //add the product promotion if (productPromotion == null) { // get the clearance promotion var promotion = _unitOfWork.PromotionRepository.Get(filter: pr => pr.Name == "Clearance").FirstOrDefault(); if (promotion != null) { ProductPromotion promo = new ProductPromotion(); promo.productID = product.productID; promo.promotionID = promotion.promotionID; promo.onDate = DateTime.Now; promo.offDate = null; promo.canOverwrite = true; _unitOfWork.ProductPromotionRepository.Create(promo); _unitOfWork.SaveChanges(); } } } } 

模型绑定不适用于foreach循环。 你必须使用for循环。 这样,indexer [i]用于为生成的html中的每个输入元素创建唯一的名称属性。

 @for (int i = 0; i < Model.Count; i++) { @Html.HiddenFor(modelItem => modelItem[i].productID);  @Html.DisplayFor(modelItem => modelItem[i].sku) @Html.DisplayFor(modelItem => modelItem[i].title) @Html.DisplayFor(modelItem => modelItem[i].includeOnSite) @Html.DisplayFor(modelItem => modelItem[i].salePrice) @Html.DisplayFor(modelItem => modelItem[i].RetailPrice) @Html.DisplayFor(modelItem => modelItem[i].quantity) @Html.EditorFor(modelItem => modelItem[i].isClearance)  } 

有关模型绑定集合的详细说明,请查看本文

你需要一个模型绑定器来绑定列表,或者一个displayTemplate ..现在生成的列表项名称是:item.sku,item.title等。标准模型绑定器不能将它绑定到列表..