Пример #1
0
        private void AddToCartForSession([FromServices] CartService srv, int prdId, int quantity)
        {
            var sessionCart = HttpContext.Session.GetString("Cart");
            var cart        = new Cart();

            if (sessionCart == null)
            {
                srv.AddCartItemForSession(prdId, quantity, cart);
            }
            else
            {
                cart = JsonConvert.DeserializeObject <Cart>(sessionCart);
                var prdCart = cart.CartItems.FirstOrDefault(x => x.ProductId == prdId);
                srv.AddCartItemForSession(prdId, quantity, cart);
            }
            var strCart = JsonConvert.SerializeObject(cart);

            HttpContext.Session.SetString("Cart", strCart);
            ViewData["ItemCount"] = cart.Quantity;
        }