public IActionResult AddEditProduct(String productId) { var dto = new AddEditProductView() { Macro = new Macro() }; if (String.IsNullOrWhiteSpace(productId)) { dto.Macro.Calories = 0; dto.Macro.Carbohydrates = 0; dto.Macro.Fat = 0; dto.Macro.Protein = 0; dto.Macro.Quantity = 0; dto.Macro.QuantityType = QuantityType.Grams; dto.Mode = Mode.Add; ViewData["Title"] = "Add"; } else { var product = productManagement.GetProduct(new Guid(productId)); dto.Name = product.Name; dto.Manufacturer = product.Manufacturer; dto.ProductId = product.ProductId; dto.Type = product.Type; dto.TypeDisplayName = product.Type.GetDisplayName(); dto.Macro.Calories = product.Macro.Calories; dto.Macro.Carbohydrates = product.Macro.Carbohydrates; dto.Macro.Fat = product.Macro.Fat; dto.Macro.Protein = product.Macro.Protein; dto.Macro.Quantity = product.Macro.Quantity; dto.Macro.QuantityType = product.Macro.QuantityType; dto.Mode = Mode.Edit; ViewData["Title"] = "Edit"; } return View(dto); }
public IActionResult AddEditProduct(AddEditProductView dto) { try { var product = new ProductDto() { Macro = dto.Macro, Name = dto.Name, Manufacturer = dto.Manufacturer, Type = dto.Type, TypeDisplayName = dto.Type.GetDisplayName() }; if (dto.Mode == Mode.Add) productManagement.AddProduct(product); else if (dto.Mode == Mode.Edit) { product.ProductId = dto.ProductId; productManagement.UpdateProduct(product); } } catch (Exception exc) { ModelState.AddModelError("AdditionalValidation", exc.Message); return View(dto); } return RedirectToAction("ProductList", "Diet"); }