public ActionResult AddProduct(ProductVM productvm, HttpPostedFileBase file) { int id; if (!ModelState.IsValid) { productvm.Categories = new SelectList(_categoryservice.GetAllCategories(), "Id", "Name"); return(View(productvm)); } else { Product product = new Product(); product.Name = productvm.Name; product.Description = productvm.Description; product.Price = productvm.Price; Category selectedcategory = _categoryservice.GetAllCategories().Where(x => x.Id == productvm.CategoryId).FirstOrDefault(); product.category = selectedcategory; product.CategoryId = selectedcategory.Id; product.CategoryName = selectedcategory.Name; if (file != null) { product.ImageName = file.FileName; } _productservice.CreateProduct(product); TempData["SM"] = "You have added a product"; id = product.Id; // Check if a file was uploaded if (file != null && file.ContentLength > 0) { _imageProcessing.GenerateDirectoriesAndSaveImages(file, id); } else { Category _selectedcategory = _categoryservice.GetAllCategories().Where(x => x.Id == productvm.CategoryId).FirstOrDefault(); productvm.Categories = new SelectList(_categoryservice.GetAllCategories(), "Id", "Name"); productvm.category = _selectedcategory; productvm.CategoryId = _selectedcategory.Id; productvm.CategoryName = _selectedcategory.Name; //ModelState.AddModelError("", "No image was not uploaded - something went wrong"); return(this.RedirectToAction <ShopController>(c => c.Products(1, null))); } } return(this.RedirectToAction <ShopController>(c => c.Products(1, null))); }