Exemple #1
0
        public void AddToCart(Product product, int count=1)
        {
            var cartItem = dbContext.Carts.SingleOrDefault(
                c => c.CartId == shoppingCartId && c.ProductId == product.Id);

            if(cartItem==null)
            {
                //Create a new cart item
                cartItem = new Cart
                {
                    ProductId = product.Id,
                    CartId = shoppingCartId,
                    Count = count,
                    DateCreated = DateTime.Now
                };
                dbContext.Carts.Add(cartItem);
            }

            else
            {
                //Addd one to the quantities
                cartItem.Count+=count;
            }

            dbContext.SaveChanges();
        }
        public ActionResult Add(Product product,HttpPostedFileBase picThumbnail, HttpPostedFileBase picDetail)
        {
            string picThumbailPath = Path.Combine(Server.MapPath("~/img"), product.PicPathThumbnail);
            string picDetailPath = Path.Combine(Server.MapPath("~/img"), product.PicPathDeatil);
            if (System.IO.File.Exists(picThumbailPath))
            {
                ModelState.AddModelError("", "File " + product.PicPathThumbnail + " already exists.");
            }
            if (System.IO.File.Exists(picDetailPath))
            {
                ModelState.AddModelError("", "File " + product.PicPathDeatil + " already exists.");
            }
            if (ModelState.IsValid)
            {

                try
                {
                    if (picThumbnail !=null && picThumbnail.ContentLength>0)
                    {
                        picThumbnail.SaveAs(picThumbailPath);

                    }
                    if (picDetail != null && picDetail.ContentLength > 0)
                    {
                        picDetail.SaveAs(picDetailPath);

                    }
                    product.PicPathThumbnail = "/img/" + product.PicPathThumbnail;
                    product.PicPathDeatil = "/img/" + product.PicPathDeatil;
                    dbContext.Products.Add(product);
                    dbContext.SaveChanges();
                    TempData["message"] = "Successfully Added a product.";
                    return RedirectToAction("Index");

                }
                catch
                {
                    ModelState.AddModelError("", "Something is wrong, try again");
                }
            }
            return View(product);
        }
        public ActionResult EditProduct(Product updatedProduct)
        {
            if (ModelState.IsValid)
            {
                Product product = dbContext.Products.FirstOrDefault(p => p.Id == updatedProduct.Id);
                if (product==null)
                {
                    TempData["Message"] = "Invalid Product Id";
                    return RedirectToAction("Index");
                }
                product.CatalogNumber = updatedProduct.CatalogNumber;
                product.ProductName = updatedProduct.ProductName;
                product.ProductCategoryId = updatedProduct.ProductCategoryId;
                product.ProductDescription = updatedProduct.ProductDescription;
                product.ProductPackageSize = updatedProduct.ProductPackageSize;
                product.ProductUnitPrice = updatedProduct.ProductUnitPrice;
                product.PackageUnit = updatedProduct.PackageUnit;
                product.Stock = updatedProduct.Stock;
                this.dbContext.Entry(product).State = EntityState.Modified;

                try
                {
                    dbContext.SaveChanges();
                    return RedirectToAction("Index");

                }
                catch
                {
                    ModelState.AddModelError("", "Something is wrong, try again");
                }
            }
            return View(updatedProduct);
        }