コード例 #1
0
        public IActionResult AddProductToCart(int productId, int quantity, int cartId)
        {
            var repo = new ProductsRepository(_connectionString);

            if (cartId != 0)
            {
                ShoppingCartProducts scp = new ShoppingCartProducts
                {
                    ProductId      = productId,
                    ShoppingCartId = cartId,
                    Quantity       = quantity
                };
                bool b = repo.AddProductToCart(scp);
                return(Json(b));
            }
            else
            {
                int cId = repo.AddCart();
                ShoppingCartProducts scp = new ShoppingCartProducts
                {
                    ProductId      = productId,
                    ShoppingCartId = cId,
                    Quantity       = quantity
                };
                bool b = repo.AddProductToCart(scp);
                HttpContext.Session.Set("cartId", cId);
                return(Json(b));
            }
        }