我在mvc中用购物车建立了一个网站,如何更新购物车数量

时间:2019-02-22 10:55:58

标签: model-view-controller

当客户选择产品时,他会转到页面上,向他显示他的选择以及有关该产品的详细信息。一切都很好,如果他再次选择该产品,我会做出求和的动作,现在我想要客户选择产品的数量2,然后按添加到购物车,该操作将执行相同的操作。我该怎么做? 视图:enter image description here

enter image description here

和此处的代码:

 <div class="product_count">
                    <label for="qty">Quantity:</label>
                    <input type="text" name="qty" id="sst" maxlength="12" value="1" title="Quantity:" class="input-text qty">
                    <button onclick="var result = document.getElementById('sst'); var sst = result.value; if( !isNaN( sst )) result.value++;return false;"
                            class="increase items-count" type="button">
                        <i class="lnr lnr-chevron-up"></i>
                    </button>
                    <button onclick="var result = document.getElementById('sst'); var sst = result.value; if( !isNaN( sst ) &amp;&amp; sst > 0 ) result.value--;return false;"
                            class="reduced items-count" type="button">
                        <i class="lnr lnr-chevron-down"></i>
                    </button>
                </div>
                <div class="card_area d-flex align-items-center">
                    <a class="primary-btn" href='@Url.Action("AddToCart", "ShoppingCart",new { id=singleshoes.ShoesID })'>Add to Cart</a>
                    <a class="icon_btn" href="#"><i class="lnr lnr lnr-diamond"></i></a>
                    <a class="icon_btn" href="#"><i class="lnr lnr lnr-heart"></i></a>
                </div>

这里是动作

  public ActionResult AddToCart(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        if (Session[strCart] == null)
        {
            List<Cart> lsCart = new List<Cart>
            {
                new Cart(db.Shoes.Find(id),1)

            };
        Session[strCart] = lsCart;
        }
        else
        {
            List<Cart> lsCart = (List<Cart>)Session[strCart];
            int check = isExistingCheck(id);
            if (check == -1)
            {
                lsCart.Add(new Cart(db.Shoes.Find(id), 1));
            }
            else
            {
                lsCart[check].Quantity++;
            }

            Session[strCart] = lsCart;
        }
        return View("Cart");
    }

    private int isExistingCheck(int? id)
    {
        List<Cart> lsCart = (List<Cart>)Session[strCart];
        for (int i = 0; i < lsCart.Count; i++)
        {
            if (lsCart[i].Shoes.ShoesID == id) return i;
        }
        return -1;
    }

0 个答案:

没有答案