Ejemplo n.º 1
0
        public IActionResult Buy(int id)
        {
            Product product = new Product();

            if (SessionExtensions.GetComplexData <List <Cart> >(HttpContext.Session, "cart") == null)
            {
                List <Cart> cart = new List <Cart>();
                cart.Add(new Cart(_context.Products.Where(p => p.ProductId == id).ToList().ElementAt(0), 1));
                SessionExtensions.SetComplexData(HttpContext.Session, "cart", cart);
                ViewBag.cartCount     = 1;
                ViewData["cartCount"] = 1;
            }
            else
            {
                List <Cart> cart  = SessionExtensions.GetComplexData <List <Cart> >(HttpContext.Session, "cart");
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Cart(_context.Products.Where(p => p.ProductId == id).ToList().ElementAt(0), 1));
                }
                SessionExtensions.SetComplexData(HttpContext.Session, "cart", cart);
                ViewBag.cartCount = cart.Count;
            }
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 2
0
        public IActionResult Remove(int id)
        {
            List <Cart> cart  = SessionExtensions.GetComplexData <List <Cart> >(HttpContext.Session, "cart");
            int         index = isExist(id);

            cart.RemoveAt(index);
            SessionExtensions.SetComplexData(HttpContext.Session, "cart", cart);
            return(RedirectToAction("Index"));
        }