Пример #1
0
        public ActionResult addCart(cartView item)
        {
            if (Request.Cookies["cartItem"] == null)
            {
                cartView cv = new cartView {
                    idPro = item.idPro, quantity = item.quantity, name = item.name, price = item.price
                };
                List <cartView> lcv = new List <cartView>();
                string          rs  = JsonConvert.SerializeObject(lcv);
                HttpCookie      ck  = new HttpCookie("cartItem", rs);
                ck.Expires.AddDays(2);
                Response.Cookies.Add(ck);
            }

            else
            {
                string          rs  = Request.Cookies["cartItem"].Value;
                List <cartView> lcv = JsonConvert.DeserializeObject <List <cartView> >(rs);
                cartView        obj = lcv.FirstOrDefault(x => x.idPro == item.idPro);
                if (obj != null)
                {
                    obj.quantity += 1;
                }
                else
                {
                    cartView cv = new cartView {
                        idPro = item.idPro, quantity = item.quantity, name = item.name, price = item.price
                    };
                    lcv.Add(cv);
                }
                rs = JsonConvert.SerializeObject(lcv);
                Response.Cookies["cartItem"].Value = rs;
            }
            return(RedirectToAction("Product", new { id = item.idPro }));
        }
Пример #2
0
        public ActionResult deleteCart(int id)
        {
            string          rs   = Request.Cookies["cartItem"].Value;
            List <cartView> lcv  = JsonConvert.DeserializeObject <List <cartView> >(rs);
            cartView        item = lcv.Single(r => r.idPro == id);

            lcv.Remove(item);
            rs = JsonConvert.SerializeObject(lcv);
            Response.Cookies["cartItem"].Value = rs;
            return(RedirectToAction("Cart"));
        }