This project is read-only.

New cart created each time even when the same item is added to the Shopping Cart.

Mar 10 at 2:16 PM
Edited Mar 10 at 2:26 PM
What should happen is when the first ever product of its kind is added to the cart, a new cart object is created, then if the same item is added to the shopping cart, the cart object with the same ProductId is fetched and the count variable is incremented.

For some reason, I keep creating a new cart even for the same product being added to the Shopping Cart.

Here's the controller method to AddToCart:
        public ActionResult AddToCart(int id)
            var addedProduct = db.Products.Single(p => p.Id == id);
            var productName = addedProduct.Name;

            var cart = ShoppingCart.GetShoppingCart(this.HttpContext);


            var results = new ShoppingCartAddViewModel
                Message = Server.HtmlEncode(productName) +
                    " has been added to your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount()

            return Json(results);
Here is the actual AddItemToCart method:
public void AddItemToCart(Product product)
            var cartItem = db.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId &&
                                                     c.ProductId == product.Id);

            if (cartItem == null)
                cartItem = new Cart
                    ProductId = product.Id,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now, 
                    Product = product