我不确定是什么问题,因为我对MVC很新。这是一个购物车。客户可以查看他们的购物车并编辑数量。
在HttpPost ViewCart方法中,购物车始终为空,行数为零。
控制器:
public ActionResult ViewCart() {
var cart = (CartViewModel)Session["Cart"];
return View(cart);
}
[HttpPost]
public ActionResult ViewCart(CartViewModel cart) {
Session["Cart"] = cart;
return RedirectToAction("Order", "Checkout");
}
查看:
@model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
@foreach (var item in Model.Lines) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Article.Description)</td>
<td>@Html.EditorFor(modelItem => item.Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}
视图模型:
public class CartViewModel {
public List<Line> Lines { get; set; }
public CartViewModel() {
Lines = new List<Line>();
}
}
答案 0 :(得分:0)
尝试更改视图以使用索引:
@model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
@for (int i = 0; i < Model.Lines.Count; i++) {
<tr>
<td>@Html.DisplayFor(m => Model.Lines[i].Article.Description) @Html.HiddenFor(m => Model.Lines[i].Article.Id)</td>
<td>@Html.EditorFor(m => Model.Lines[i].Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}