public ActionResult AddProduct(ProductViewModel model, HttpPostedFileBase file) { Product sensitiveData = null; // Check model state if (!ModelState.IsValid) { // Init model ProductViewModel fetchedData = null; var config = new MapperConfiguration(cfg => { cfg.CreateMap <ProductViewModel, Product>(); }); IMapper mapper = config.CreateMapper(); sensitiveData = mapper.Map <ProductViewModel, Product>(fetchedData); shopBL.AddProduct(sensitiveData); Product fetchedDatas = null; var configs = new MapperConfiguration(cfg => { cfg.CreateMap <Product, ProductViewModel>(); }); IMapper mappers = config.CreateMapper(); ProductViewModel sensitiveDatas = mapper.Map <Product, ProductViewModel>(fetchedDatas); return(View(model)); } if (!shopBL.AddProduct(sensitiveData)) { // Make sure product name is unique ModelState.AddModelError("", "That product name is taken!"); return(View(model)); } // Declare product id int id; Product product = new Product(); product.Name = model.Name; product.Slug = model.Name.Replace(" ", "-").ToLower(); product.Description = model.Description; product.Price = model.Price; product.CatagoryId = model.CatagoryId; Category category = shopBL.SaveProduct(product); product.CatagoryName = category.Name; // Get the id id = product.Id; // Set TempData message TempData["SM"] = "You have added a product!"; #region Upload Image // Create necessary directories var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); var pathString1 = Path.Combine(originalDirectory.ToString(), "Products"); var pathString2 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString()); var pathString3 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs"); var pathString4 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery"); var pathString5 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs"); if (!Directory.Exists(pathString1)) { Directory.CreateDirectory(pathString1); } if (!Directory.Exists(pathString2)) { Directory.CreateDirectory(pathString2); } if (!Directory.Exists(pathString3)) { Directory.CreateDirectory(pathString3); } if (!Directory.Exists(pathString4)) { Directory.CreateDirectory(pathString4); } if (!Directory.Exists(pathString5)) { Directory.CreateDirectory(pathString5); } // Check if a file was uploaded if (file != null && file.ContentLength > 0) { // Get file extension string ext = file.ContentType.ToLower(); // Verify extension if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "image/png") { ModelState.AddModelError("", "The image was not uploaded - wrong image extension."); return(View(model)); } } // Init image name string imageName = file.FileName; Product productImage = shopBL.UploadImage(id); productImage.ImageName = imageName; // Set original and thumb image paths var path = string.Format("{0}\\{1}", pathString2, imageName); var path2 = string.Format("{0}\\{1}", pathString3, imageName); // Save original file.SaveAs(path); // Create and save thumb WebImage img = new WebImage(file.InputStream); img.Resize(300, 250); img.Save(path2); #endregion // Redirect return(RedirectToAction("AddProduct")); }