u010220872 2014-07-03 15:27 采纳率: 0%
浏览 2559

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一直为空绑定不到。这是哪里出了问题呢?

  • 写回答

2条回答 默认 最新

  • QQ530183726 2015-09-22 23:53
    关注

    @{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)


    }
    这是我参照数上写的代码,运行正常~你可以参考下!
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组