问题描述
- MVC模型绑定遇到的问题
-
在实现购物车时,我的其他功能都可以正常运行,只有更新购物车数量这个功能中Carts绑定不到
视图代码:@model IEnumerable<MVCShopping.Models.Cart> @{ var ajaxOption = new AjaxOptions() { OnSuccess = "RemoveCartSuccess", OnFailure = "RemoveCartFailure", Confirm = "您确定要从购物车删除吗?", HttpMethod = "Post", }; } @section scripts{ @Scripts.Render("~/bundles/jqueryval") <script> function RemoveCartSuccess() { alert('移出购物从成功'); location.reload(); } function RemoveCartFailure(xhr) { alert('移出购物车失败(Http状态代吗:' + xhr.status); } </script> } <h2>购物车列表</h2> @using (Html.BeginForm("UpdateAmount", "Cart")) { <table> <tr> <th>产品名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th></th> </tr> @{int subTotal = 0;} @foreach (var item in Model) { subTotal += item.Product.Price * item.Amount; var ddlAmountList = new SelectList(Enumerable.Range(1,10),item.Amount); @Html.Hidden(item.Product.Id.ToString()) <tr> <td>@Html.DisplayFor(t=>item.Product.Name)</td> <td>NT¥@(item.Product.Price)</td> <td>@Html.DropDownListFor(t=>item.Amount,ddlAmountList)</td> <td>NT¥@(item.Product.Price*item.Amount)</td> <td> @Ajax.ActionLink("删除","Remove",new{ProductId=item.Product.Id},ajaxOption) </td> </tr> } <tr> <th></th> <th></th> <th>总价</th> <th id="subtotal">NT¥ @subTotal</th> <th></th> </tr> </table> <p> <input type="submit" value="更新数量" /> <input type="button" value="完成订单" onclick="location.href='@Url.Action("Complete","Order")';" /> </p> }
控制器CartControl代码:
public class CartController : Controller { //非会员也可使用所以购物车保存在Session中 // 显示当前购物从项目 MvcShoppingContext db = new MvcShoppingContext(); List<Cart> Carts { get { if (Session["Carts"] == null) { Session["Carts"] = new List<Cart>(); } return Session["Carts"] as List<Cart>; } set { Session["Carts"] = value; } } public ActionResult Index() { return View(this.Carts); } //添加产品项目到购物车,如果没有传入Amount参数则默认购买数量为1 //因为要通过Ajax调用这个Action,所以可以先标示Post属性 [HttpPost] public ActionResult AddToCart(int ProductId, int Amount = 1) { var product = db.Products.Find(ProductId); //验证产品是否存在 if (product == null) return HttpNotFound(); var existiongCart = this.Carts.FirstOrDefault(p => p.Product.Id == ProductId); if (existiongCart != null) { existiongCart.Amount += 1; } else { this.Carts.Add(new Cart() { Product=product,Amount=Amount}); } return new HttpStatusCodeResult(HttpStatusCode.Created); } //移出购物从项目 [HttpPost] public ActionResult Remove(int ProductId) { var existingCart = this.Carts.FirstOrDefault(p => p.Product.Id == ProductId); if (existingCart != null) { this.Carts.Remove(existingCart); } return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK); } //更新数量 [HttpPost] public ActionResult UpdateAmount(IEnumerable<Cart> Carts) { foreach (var item in Carts) { var existingCart = this.Carts.FirstOrDefault(p => p.Product.Id == item.Product.Id); if (existingCart != null) { existingCart.Amount = item.Amount; } } return RedirectToAction("Index","Cart"); } }
就是最后一个更新数量的方法( public ActionResult UpdateAmount(IEnumerable Carts) )上Carts一直为空绑定不到。这是哪里出了问题呢?
解决方案
@{int subTotal = 0;}
@for (int i = 0; i < Model.Count; i++)
{
subTotal += Model[i].Product.Price * Model[i].Amount;
var ddlAmountList = new SelectList(Enumerable.Range(1, 10), Model[i].Amount);
@Html.HiddenFor(modelItem => modelItem[i].Product.Id)
@Html.DisplayFor(modelItem => Model[i].Product.Name)
¥@(Model[i].Product.Price)
@Html.DropDownListFor(modelItem => Model[i].Amount, ddlAmountList)
@(Model[i].Product.Price * Model[i].Amount)元
@Ajax.ActionLink("删除", "Remove", new { ProductId = Model[i].Product.Id }, ajaxOption)
}
这是我参照数上写的代码,运行正常~你可以参考下!
解决方案二:
请问最终你解决了么?update amount获取到了list结果么?