【发布时间】:2015-07-14 14:34:32
【问题描述】:
场景是一个购物篮 - 用户将更新数量的文本框并点击更新按钮。 目前,点击更新按钮时,Action 被点击,但模型属性全部为空。
我的第一个想法是绑定存在一些问题,因为每一行的控件 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>
这是我的 displayFor 模板:
@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>
【问题讨论】:
-
您能展示一下使用
for循环时的样子吗?你能在提交表单时显示 POSTed 数据的样子吗? -
当然 - 我会在上面的帖子中添加...给我 2 分钟。
-
@StriplingWarrior - 刚刚添加了生成的 HTML。 - 忽略 - 我已经添加了 foreach 循环!!! 2分钟...
标签: c# model-view-controller model-binding