コード例 #1
0
ファイル: ShoppingCart.cs プロジェクト: achilus/Cuisine
        public void AddToCart(Product product)
        {
            // Get the matching cart and Product instances
            var cartItem = storeDB.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId  && c.ProductId == product.ProductId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    ProductId = product.ProductId,
                    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.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId);
            return View(product);
        }
コード例 #3
0
ファイル: ProductController.cs プロジェクト: achilus/Cuisine
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0 && Request.Files[0] != null) {
                    string filePath = Path.Combine(HttpContext.Server.MapPath("~/Content/ProductImages"),
                        Path.GetFileName(Request.Files[0].FileName));
                    Request.Files[0].SaveAs(filePath);
                    product.ProductImageUrl = filePath;
                }
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId);
            return View(product);
        }
コード例 #4
0
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryId);
     return View(product);
 }