MVC模型绑定问题-对象列表 [英] MVC Model binding issue - list of objects

查看:83
本文介绍了MVC模型绑定问题-对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方案是一个购物篮-用户将更新数量的文本框,然后单击更新"按钮. 当前,在单击更新"按钮时,将单击动作",但模型属性全部为空.

The scenario is a shopping basket - the user will update a text box for the quantity and hit the update button. Currently, upon hitting the update button, the Action is hit but the model properties are all null.

我的第一个想法是绑定存在一些问题,因为控件的id对于每一行都发生了更改.

My first thoughts are that there is some issue with the binding because the ids of the controls are changed for each row.

我的想法正确吗?如果是这样,我该如何解决?

Are my thoughts correct? if so, how can I resolve this?

我也尝试使用for循环,而不是foreach,但这也不起作用. 我所有的代码如下:

I also tried to use a for loop, rather than foreach but that didn't work either. All my code is below:

<table class="order-table order-table-nomargin order-table-noborder hidden-xs hidden-sm">
  <thead>
    <tr><th>Products</th><th></th><th>Price</th><th>Qty.</th><th>Total</th></tr>
  </thead>
  <tbody>
    @foreach (Nop.Web.Models.Custom.CustomOrderLine ol in Model.Cart.OrderLines)
    {    
      @Html.DisplayFor(m => ol)
    }      
  </tbody>
</table> 

这是我的显示用于模板:

This is my displayFor Template:

@model Nop.Web.Models.Custom.CustomOrderLine
<tr>
    <td>
        <img alt="book image" src="@Model.Image.Media.Url"  width="100" />
    </td>
    <td>
        @Html.ActionLink(@Model.Title, "Product", "Catalog", new { seName = @Model.SeName }, null)<br />
        @*@Html.DisplayFor(m => m.Title)<br />*@ By: @Html.DisplayFor(m => m.Author)<br />
        Published date: @Html.DisplayFor(m => m.PublishedDate)<br />
        Format: @Html.DisplayFor(m => m.Format)
    </td>
    <td>
        <p class="onlinePrice">
            <em>@Html.DisplayFor(m => m.PriceWithDiscount)</em></p>
        <p class="saving">
            <em>@Html.DisplayFor(m => m.PriceDiscount)</em></p>
        <p class="rrp">
            RRP: <em>@Html.DisplayFor(m => m.Price)</em></p>
    </td>
    <td>
        @using (Html.BeginForm("UpdateCart", "CustomCart"))
        { 
            string test = Model.Isbn13;
            int QuantTest = Model.Qty;

            @Html.EditorFor(m => Model.Qty)
            @Html.HiddenFor(m => m.Isbn13)
            //@Html.TextBoxFor(m => m.Qty)
            <input type="submit" value="Update" />
        }
    </td>
    <td>
        <p class="subTotal numeric-right">@Html.DisplayFor(m => m.Total)</p>
    </td>
</tr>

我的控制器操作:

[HttpPost]
public ActionResult UpdateCart(CustomOrderLine model)
{

    //add code here


    return XXX;
}

以下是使用foreach循环生成的HTML:

Here's the generated HTML using the foreach loop:

<table class="order-table order-table-nomargin order-table-noborder hidden-xs hidden-sm">
<thead>
<tr><th>Products</th><th></th><th>Price</th><th>Qty.</th><th>Total</th></tr>
</thead>
<tbody>
<tr>
    <td>
        <img alt="book image" src="http://media.harrypotter.bloomsbury.com/rep/s/9781408855652_309039.jpeg"  width="100" />
    </td>
    <td>
        <a href="/uk/harry-potter-and-the-philosophers-stone-9780747558194-9781408855652">Harry Potter and the Philosopher&#39;s Stone </a><br />
         By: J.K. Rowling<br />
        Published date: 01-09-2014<br />
        Format: Paperback
    </td>
    <td>
        <p class="onlinePrice">
            <em>6.29</em></p>
        <p class="saving">
            <em>Save ВЈ0.70 (10%)</em></p>
        <p class="rrp">
            RRP: <em>6.99</em></p>
    </td>
    <td>

<form action="/CustomCart/UpdateCart" method="post">
<input class="text-box single-line" data-val="true" data-val-number="The field Qty must be a number." data-val-required="&amp;#39;Qty&amp;#39; must not be empty." id="ol_Qty" name="ol.Qty" type="text" value="1" />
<input id="ol_Isbn13" name="ol.Isbn13" type="hidden" value="9781408855652" />            
<input type="submit" name="123" id="123" value="Update 2" />
</form>    </td>
    <td>
        <p class="subTotal numeric-right">6.29</p>
    </td>
</tr>
</tbody>
</table> 

推荐答案

首先,如果您希望在控制器操作中接收到多个CustomOrderLine,则需要列出项目列表.在这种情况下,使用Cart似乎更合适,它上面有一个CustomOrderLine的列表:

First of all, if you want multiple CustomOrderLines to be received in your controller action, you're going to need to take a list of items. In this case, it seem like it's probably more appropriate to take the Cart, which has a list of CustomOrderLines on it:

[HttpPost]
public ActionResult UpdateCart(Cart cart)
{
    //add code here
    return XXX;
}

MVC模型绑定从看起来像这样的参数中填充列表:

MVC model binding populates lists from parameters that look like this:

cart.OrderLines[0].ItemId = 123
cart.OrderLines[0].Qty = 1
cart.OrderLines[1].ItemId = 456
cart.OrderLines[1].Qty = 2

因此,为了使HTML输入的name属性与该模式匹配,需要为EditorFor提供一个表达式,以帮助其知道如何构造该名称:

So in order for the name attributes of your HTML inputs to match this pattern, EditorFor needs to be given an expression that will help it know how to construct this name:

@for (int i=0; i<Model.Cart.OrderLines.Length; i++)
{    
  @Html.EditorFor(m => m.Cart.OrderLines[i])
}  

您会发现我正在使用EditorFor,因为看来您正在显示订单的可编辑版本.如果这样做,您将需要将显示模板移到用于编辑器模板的位置.

You'll notice that I'm using EditorFor, since it appears that you're showing an editable version of your orders. If you do this, you will want to move your display template into the location for editor templates instead.

这篇关于MVC模型绑定问题-对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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