MVC 模型绑定问题 - 对象列表
MVC Model binding issue - list of objects
场景是购物篮 - 用户将更新数量的文本框并点击更新按钮。
目前,点击更新按钮后,点击了操作,但模型属性全部为空。
我的第一个想法是绑定存在一些问题,因为每一行的控件 ID 都发生了变化。
我的想法对吗?如果是这样,我该如何解决?
编辑:我还尝试使用 for 循环而不是 foreach,但这也不起作用。
我的所有代码如下:
<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>
这是我的展示模板:
@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:
<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'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="&#39;Qty&#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
的列表:
[HttpPost]
public ActionResult UpdateCart(Cart cart)
{
//add code here
return XXX;
}
MVC 模型绑定从如下所示的参数填充列表:
cart.OrderLines[0].ItemId = 123
cart.OrderLines[0].Qty = 1
cart.OrderLines[1].ItemId = 456
cart.OrderLines[1].Qty = 2
因此,为了使 HTML 输入的 name
属性匹配此模式,EditorFor
需要提供一个表达式,帮助它知道如何构造此名称:
@for (int i=0; i<Model.Cart.OrderLines.Length; i++)
{
@Html.EditorFor(m => m.Cart.OrderLines[i])
}
您会注意到我使用的是 EditorFor
,因为看起来您显示的是订单的可编辑版本。如果这样做,您将需要将显示模板移至编辑器模板所在的位置。
如果您想保持单行更新,请将您的控制器动作签名更改为:
[HttpPost]
public ActionResult UpdateCart(CustomOrderLine ol)
{
return XXX;
}
这将匹配 DisplayFor(m => ol)
中呈现的前缀。
场景是购物篮 - 用户将更新数量的文本框并点击更新按钮。 目前,点击更新按钮后,点击了操作,但模型属性全部为空。
我的第一个想法是绑定存在一些问题,因为每一行的控件 ID 都发生了变化。
我的想法对吗?如果是这样,我该如何解决?
编辑:我还尝试使用 for 循环而不是 foreach,但这也不起作用。 我的所有代码如下:
<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>
这是我的展示模板:
@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:
<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'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="&#39;Qty&#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
的列表:
[HttpPost]
public ActionResult UpdateCart(Cart cart)
{
//add code here
return XXX;
}
MVC 模型绑定从如下所示的参数填充列表:
cart.OrderLines[0].ItemId = 123
cart.OrderLines[0].Qty = 1
cart.OrderLines[1].ItemId = 456
cart.OrderLines[1].Qty = 2
因此,为了使 HTML 输入的 name
属性匹配此模式,EditorFor
需要提供一个表达式,帮助它知道如何构造此名称:
@for (int i=0; i<Model.Cart.OrderLines.Length; i++)
{
@Html.EditorFor(m => m.Cart.OrderLines[i])
}
您会注意到我使用的是 EditorFor
,因为看起来您显示的是订单的可编辑版本。如果这样做,您将需要将显示模板移至编辑器模板所在的位置。
如果您想保持单行更新,请将您的控制器动作签名更改为:
[HttpPost]
public ActionResult UpdateCart(CustomOrderLine ol)
{
return XXX;
}
这将匹配 DisplayFor(m => ol)
中呈现的前缀。