gpt4 book ai didi

c# - 需要将 List 传递给 Controller ​​中的 Http Post

转载 作者:行者123 更新时间:2023-12-02 11:12:32 28 4
gpt4 key购买 nike

我正在将一个列表传递给 View 。在 View 中我需要传回该列表。它包括一个可编辑的复选框,这是我真正需要的项目。显示的所有其他字段仅供只读用途。如果用户想要将该项目指定为间隙,那么他们会选中 isClearance 复选框。因此,当他们点击保存按钮时,它会点击 HttpPost Index()。然而,它传回一个空值。如果我将其更改为模型对象,而不是列表,它就可以正常工作。

这是 Controller 索引:

 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);
}

这是 View :

@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>

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

        [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();
}
}
}
}

最佳答案

模型绑定(bind)不适用于 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>
}

有关模型绑定(bind)集合的详细说明,请查看 this article

关于c# - 需要将 List<model> 传递给 Controller ​​中的 Http Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14163281/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com