public void UpdateProduct(ProductViewModel productViewModel) { var isNew = false; var product = _dbContext.Products .SingleOrDefault(p => p.UniqueName == productViewModel.UniqueName); if (product == null) { isNew = true; product = new Product(); } product.CategoryId = productViewModel.CategoryId; product.UniqueName = productViewModel.UniqueName; product.Color = productViewModel.Color; product.Description = productViewModel.Description; product.ImagePath = productViewModel.ImagePath; product.SortOrder = productViewModel.SortOrder; if (isNew) _dbContext.Products.Add(product); _dbContext.SaveChanges(); }
public IActionResult Edit(string uniqueName) { var product = _productService.GetProductBy(uniqueName); var productViewModel = new ProductViewModel() { CategoryId = product.CategoryId, UniqueName = product.UniqueName, Color = product.Color, Description = product.Description, SortOrder = product.SortOrder, ImagePath = product.ImagePath }; var categoryItemsList = _productService.GetCategories() .Select(c => new SelectListItem() { Text = c.Name, Value = c.CategoryId.ToString(), Selected = product.CategoryId == c.CategoryId }).ToArray(); var vm = new ProductEditViewModel { Categories = categoryItemsList, Product = productViewModel }; return View(vm); }
public void CreateProduct(ProductViewModel productViewModel) { var product = new Product { CategoryId = productViewModel.CategoryId, UniqueName = productViewModel.UniqueName, Color = productViewModel.Color, Description = productViewModel.Description, ImagePath = productViewModel.ImagePath, SortOrder = productViewModel.SortOrder }; _dbContext.Products.Add(product); _dbContext.SaveChanges(); }