public void AddToCart(ItemsHire itemsHire) { // Get the matching cart and album instances var cartItem = db.Carts.SingleOrDefault( c => c.CartID == ShoppingCartId && c.ProductID == itemsHire.ProductID && c.Image == itemsHire.Image ); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { ProductID = itemsHire.ProductID, Image = itemsHire.Image, CartID = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity cartItem.Count++; } // Save changes db.SaveChanges(); }
public Cart(ItemsHire itemsHire, int quantity) { ItemsHire = itemsHire; Quantity = quantity; }