Example #1
0
        public JsonResult AddToCart(string id)
        {
            var     response = new { Code = 1, Msg = "Fail", sluong = @Session["sluong"], ttien = @Session["TongTien"] };
            var     db       = new SmartShopConnectionDB();
            Product sp       = db.Single <Product>("select * from Product where MaSP = @0", id);

            if (sp != null)
            {
                DHangModels objCart = (DHangModels)Session["Cart"];
                if (objCart == null)
                {
                    objCart = new DHangModels();
                }

                ShoppingCartItem item = new ShoppingCartItem()
                {
                    MaSP     = sp.MaSP,
                    TenSP    = sp.TenSP,
                    Hinh     = sp.Hinh,
                    Gia      = Int32.Parse(sp.Gia),
                    SoLuong  = 1,
                    TongCong = Int32.Parse(sp.Gia),
                };

                objCart.AddToCart(item);
                Session["Cart"] = objCart;
                ShoppingCartModels model = new ShoppingCartModels();
                model.Cart = (DHangModels)Session["Cart"];
                int ii       = 0;
                int tongtien = 0;
                foreach (var item2 in model.Cart.ListItem)
                {
                    tongtien = tongtien + (int)item2.TongCong;
                    ii++;
                }
                Session["sluong"]   = ii;
                Session["TongTien"] = tongtien.ToString("N0");
                response            = new { Code = 0, Msg = "Success", sluong = @Session["sluong"], ttien = @Session["TongTien"] };
            }

            return(Json(response));
        }