需要传递List到控制器中的Http Post
本文关键字:控制器 Post Http model List | 更新日期: 2023-09-27 17:50:52
我将List传递给视图。在视图中,我需要传递回那个列表。它包括一个可编辑的复选框,这是我真正需要的项目。显示的所有其他字段都用于只读目的。如果用户希望将项目指定为清除项,则选中isClearance复选框。所以当他们点击保存按钮时它会点击HttpPost Index()。但是,它传回一个空值。如果我把它改成modelObject,而不是List,它就能正常工作。
这里是控制器索引:
public ActionResult Index()
{
List<ClearanceViewModel> cc = new List<ClearanceViewModel>();
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<PMS.Models.ClearanceViewModel>
@{
ViewBag.title = "Clearance List";
}
<fieldset>
<legend><span>Clearance List</span></legend>
@using (Html.BeginForm())
{
if (Model.Count() > 0)
{
<table class="list">
<tr>
<th>Sku</th>
<th>Title</th>
<th>Include On Site</th>
<th>Sale price</th>
<th>Retail Price</th>
<th>Quantity</th>
<th>Clearance</th>
</tr>
@foreach(var item in Model)
{
@Html.HiddenFor(modelItem => item.productID);
<tr>
<td>@Html.DisplayFor(modelItem => item.sku)</td>
<td>@Html.DisplayFor(modelItem => item.title)</td>
<td>@Html.DisplayFor(modelItem => item.includeOnSite)</td>
<td>@Html.DisplayFor(modelItem => item.salePrice)</td>
<td>@Html.DisplayFor(modelItem => item.RetailPrice)</td>
<td>@Html.DisplayFor(modelItem => item.quantity)</td>
<td>@Html.EditorFor(modelItem => item.isClearance)</td>
</tr>
}
</table>
<p>
<input type="submit" value="Save" />
</p>
}
}
</fieldset>
这是在控制器中的HttpPost (clearanceModel是空的,当它到达foreach循环,当它应该有一个项目的列表):
[HttpPost]
public ActionResult Index(List<ClearanceViewModel> 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循环。这样,索引器[i]
被用来为生成的html中的每个输入元素创建唯一的名称属性。
@for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(modelItem => modelItem[i].productID);
<tr>
<td>@Html.DisplayFor(modelItem => modelItem[i].sku)</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].title)</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].includeOnSite)</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].salePrice)</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].RetailPrice)</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].quantity)</td>
<td>@Html.EditorFor(modelItem => modelItem[i].isClearance)</td>
</tr>
}
有关模型绑定集合的详细说明,请查看
您需要一个模型绑定器来绑定列表,或者一个displayTemplate..现在为列表项生成的名称是:item。sku,项目。标题等。标准的模型绑定器不能将其绑定到列表。