示例#1
0
        public void AddToCart(Product product)
        {
            // Get the matching cart and product instances
            var cartItem = storeDB.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                && c.ProductId == product.Id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    ProductId = product.Id,
                    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(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Categories = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId);
            ViewBag.BrandId = new SelectList(db.Brands, "BrandId", "Name", product.BrandId);
            return View(product);
        }
示例#3
0
 public ActionResult Edit(Product product)
 {
     if (product.CategoryId == 0)
     {
         db.Categories.Add(product.Category);
         db.SaveChanges();
         product.CategoryId = product.Category.CategoryId;
     }
     else
     {
         product.Category.CategoryId = product.CategoryId;
     }
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewData["NewCategory"] = null;
     ViewBag.Categories = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId);
     ViewBag.BrandId = new SelectList(db.Brands, "BrandId", "Name", product.BrandId);
     return View(product);
 }