Exemplo n.º 1
0
        public ActionResult Create(ProductViewModel products)
        {
            if (ModelState.IsValid)
            {


            var ValidImageTyps = new string[]
                {
                "image/gif",
                "image/jpeg",
                "image/pjqpeg",
                "image/png"
                };

                if(!ValidImageTyps.Contains(products.PrimaryImage.ContentType))
                {
                   ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image");

                   
                }

                var ProductCategory = db.Category.Find(products.CategoryID);

            
                  Products Products = new Products()
                {
                    ProductName = products.ProductName,
                    Brand = products.Brand,
                    DateAdded = DateTime.Now,
                    ProdDesc = products.ProdDesc,
                    stock = products.stock,
                    Price = products.Price,
                    ProductCategory = ProductCategory.CategoryName,
                    
                    CategoryID = products.CategoryID
                };

                if(products.PrimaryImage!=null&& products.PrimaryImage.ContentLength>0)
                {
                    var UploadDir = "/Content/images";
                        var imagePath = Path.Combine(Server.MapPath(UploadDir),products.PrimaryImage.FileName);
                    var imageUrl = Path.Combine(@"/Content/images/",products.PrimaryImage.FileName);
                    products.PrimaryImage.SaveAs(imagePath);
                    Products.PrimaryImage = imageUrl;
                    
                }


                db.Products.Add(Products);
                db.SaveChanges();
                return RedirectToAction("ListProducts");
            }

            ViewBag.CategoryID = new SelectList(db.Category, "ID", "CategoryName", products.CategoryID);
            return View(products);
        }
Exemplo n.º 2
0
 public ActionResult Edit(int id)
 {
     
     var result = db.Products.Find(id);
     Products products = new Products()
     {
         ProductName = result.ProductName,
         Brand = result.Brand,
         DateAdded = DateTime.Now,
         ProdDesc = result.ProdDesc,
         ProductCategory = result.ProductCategory,
         stock = result.stock,
             
         Price = result.Price,
     };
     if (products == null)
     {
         return HttpNotFound();
     }
     ViewBag.CategoryID = new SelectList(db.Category, "ID", "CategoryName",products.CategoryID);
     return View(products);
 }
Exemplo n.º 3
0
 public ActionResult Edit(Products products)
 {
   products.PrimaryImage = db.Products.Where(m=>m.ID== products.ID).FirstOrDefault().PrimaryImage;
     if (ModelState.IsValid)
     {
         var result = db.Products.Find(products.ID);
         result.ProductName = products.ProductName;
         result.Brand = products.Brand;
         result.ProductCategory = products.ProductCategory;
         result.ProdDesc = products.ProdDesc;
         result.PrimaryImage = products.PrimaryImage;
         result.stock = products.stock;
         result.Price = products.Price;
         result.CategoryID = products.CategoryID;
         db.SaveChanges();
         return RedirectToAction("ListProducts","Admin");
     }
     ViewBag.CategoryID = new SelectList(db.Category, "ID", "CategoryName", products.CategoryID);
     return View(products);
 }