예제 #1
0
        public void AddToCart(ItemModel Item)
        {
            // Get the matching cart and Item instances
            var cartItem = storeDB.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                && c.ItemId == Item.ItemId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new CartModel
                {
                    ItemId = Item.ItemId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                storeDB.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart, 
                // then add one to the quantity
                cartItem.Count++;
            }
            // Save changes
            storeDB.SaveChanges();
        }
예제 #2
0
 public ActionResult Create(ItemModel item)
 {
     if (ModelState.IsValid)
     {
         storeDB.Items.Add(item);
         storeDB.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(item);
 }