需要通过List< model>到控制器中的Http Post [英] Need to pass a List<model> to the Http Post in a Controller

查看:78
本文介绍了需要通过List< model>到控制器中的Http Post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将列表传递给视图.在视图中,我需要传递回该列表.它包括一个可编辑的复选框,这是我真正需要的项目.显示的所有其他字段仅用于只读目的.如果用户希望将该项目指定为间隙,则可以选中isClearance复选框.因此,当他们点击保存按钮时,它将点击HttpPost Index().但是,它传回null.如果我将其更改为只是modelObject而不是List,则可以正常工作.

I am passing a List to the view. In the view I need to pass back that list. It includes an editable check box, which are the items I really need. All of the other fields displayed are for read only purposes. If the user wants to designate the item as a clearance, then they check the isClearance checkbox. So when they hit the save button it hits the HttpPost Index(). However, it passes back a null. If I change it to be just a modelObject, not List, it works just fine.

这是控制器索引:

 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>

这是Controller中的HttpPost(当它到达foreach循环时(应该包含项列表),clearanceModel为null):

Here is the HttpPost in the Controller (clearanceModel is null when it hits the foreach loop, when it should have a list of the items):

        [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中的每个输入元素创建唯一的名称属性.

Model binding does not work with foreach loops. You have to use a for loop. That way the indexer [i] is used to create unique name properties for each input element in the generated 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>
        }

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

For a nice explanation of model binding collections, check out this article

这篇关于需要通过List&lt; model&gt;到控制器中的Http Post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆