public async Task <ActionResult> Create() { var viewModel = new ProductViewModelCreate(); var allCategory = await _categoryService.GetAllCategories(); viewModel.CategoryList = allCategory.Select(ConvertCategoryDTOToViewModel); var allSuppliers = await _supplierService.GetAllSuppliers(); viewModel.SupplierList = allSuppliers.Select(ConvertSuppliersDTOToViewModel); return(View("Create", viewModel)); }
private ProductDTO ConvertCreateViewModelToDTO(ProductViewModelCreate inputViewModel) { if (inputViewModel == null) { return(null); } return(new ProductDTO { ProductName = inputViewModel.ProductName, QuantityPerUnit = inputViewModel.QuantityPerUnit, ReorderLevel = inputViewModel.ReorderLevel, UnitPrice = inputViewModel.UnitPrice, UnitsInStock = inputViewModel.UnitsInStock, UnitsOnOrder = inputViewModel.UnitsOnOrder, Discontinued = inputViewModel.Discontinued, SupplierID = inputViewModel.SupplierId, CategoryID = inputViewModel.CategoryId, }); }
public async Task <ActionResult> Create(ProductViewModelCreate product) { try { if (!ModelState.IsValid) { return(View("Create", product)); } var productDTO = ConvertCreateViewModelToDTO(product); await _productService.CreateProductAsync(productDTO); return(RedirectToAction("Index")); } catch (Exception ex) { _logger.LogError(ex, $"Something went wrong during the creation new product!"); ModelState.AddModelError(string.Empty, "Something went wrong during the creation new product!"); return(View("Create", product)); } }