public IActionResult AddProduct(ProductEditViewModel productEditViewModel) { if (ModelState.IsValid) { productEditViewModel.Product.Image = uploadFile.ProcessUploadedFile(productEditViewModel.Photo, "Products");; productEditViewModel.Product.BrandId = productEditViewModel.BrandId; productEditViewModel.Product.CategoryId = productEditViewModel.CategoryId; _productRepository.CreateProduct(productEditViewModel.Product); return(RedirectToAction("Index")); } else { var categories = _categoryRepository.AllCategories; var brands = _brandRepository.AllBrands; productEditViewModel = new ProductEditViewModel { Categories = categories.Select(c => new SelectListItem() { Text = c.Cat_Name, Value = c.Cat_Id.ToString() }).ToList(), CategoryId = categories.FirstOrDefault().Cat_Id, Brands = brands.Select(b => new SelectListItem() { Text = b.Brand_Name, Value = b.Brand_Id.ToString() }).ToList(), BrandId = brands.FirstOrDefault().Brand_Id }; } return(View(productEditViewModel)); }
public async Task <IActionResult> AddProduct(ProductEditViewModel productEditViewModel) { if (ModelState.IsValid) { productEditViewModel.Product.Image = uploadFile.ProcessUploadedFile(productEditViewModel.Photo, "Products");; productEditViewModel.Product.CategoryId = productEditViewModel.CategoryId; await _productRepository.CreateProduct(productEditViewModel.Product); return(RedirectToAction("Index")); } else { var categories = _categoryRepository.AllCategories(); productEditViewModel = new ProductEditViewModel { Categories = categories.GetAwaiter().GetResult().Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToList(), CategoryId = categories.GetAwaiter().GetResult().Select(c => c.Id).FirstOrDefault() }; } return(View(productEditViewModel)); }