Пример #1
0
        public IActionResult Buy(int id)
        {
            ProductModel productModel = new ProductModel();

            if (SessionsHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart") == null)
            {
                List <Item> cart = new List <Item>();
                cart.Add(new Item {
                    Product = productModel.find(id), Quantity = 1
                });
                SessionsHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List <Item> cart  = SessionsHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item {
                        Product = productModel.find(id), Quantity = 1
                    });
                }
                SessionsHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index"));
        }
Пример #2
0
        public IActionResult Remove(int id)
        {
            List <Item> cart  = SessionsHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
            int         index = isExist(id);

            cart.RemoveAt(index);
            SessionsHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            return(RedirectToAction("Index"));
        }