public async Task <ActionResult> CreateProduct(IFormFile file, [Bind] AllProductsViewModel model) { try { if (ModelState.IsValid) { if (!IsUploadedFileImage(file)) { ModelState.AddModelError("Photo", "Filen är ogiltig!"); TempData["Errors"] = "Filen är ogiltig!"; model.Categories = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES); model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS); return(View(model)); } // Instantiate new product Product newProduct = new Product() { Name = model.Name, Price = model.Price, Quantity = model.Quantity, CategoryId = model.CategoryId, BrandId = model.BrandId, Description = model.Description, FullDescription = model.FullDescription, Specification = model.Specification, Discount = model.Discount, ActiveProduct = model.ActiveProduct }; // Request token var token = await webAPIToken.New(); // Store product var apiResonse = await webAPI.PostAsync(newProduct, ApiURL.PRODUCTS, token); // Deserialize API response content and get ID of newly created product var newProductId = webAPI.DeserializeJSON <AllProductsViewModel>(apiResonse.ResponseContent).Id; newProduct.Id = newProductId; // Store image in www root folder with unique product Id if (file != null) { // Set category folder name var folderName = await GetCategoryName(model.CategoryId); // Store new image ProductImage productImage = new ProductImage(environment.WebRootPath, folderName, file); newProduct.Photo = productImage.StoreImage(newProduct.Id); } // Update product with image var response = webAPI.UpdateAsync <Product>(newProduct, ApiURL.PRODUCTS + newProduct.Id, token); } else { model.Categories = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES); model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS); TempData["Errors"] = "Fyll i formuläret ordentligt"; return(View(model)); } TempData["Succesmsg"] = $"Great!! {model.Name} skapad i databasen"; return(RedirectToAction("AllProducts", "Product")); } catch { TempData["Database error"] = "Sorry!! Något gick fel när du lägger Data till databasen"; return(RedirectToAction("CreateProduct", "Product")); } }