public IActionResult Edit(int id) { Cheese editCheese = context.Cheeses.Single(c => c.ID == id); AddCheeseViewModel addCheeseViewModel = new AddCheeseViewModel { Name = editCheese.Name, Description = editCheese.Description, CheeseId = editCheese.ID, Title = "Edit" }; addCheeseViewModel.AddCategories(context.Categories.ToList()); return(View("Add", addCheeseViewModel)); }
public IActionResult Edit(int id, AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { Cheese editCheese = context.Cheeses.Single(c => c.ID == id); editCheese.Name = addCheeseViewModel.Name; editCheese.Description = addCheeseViewModel.Description; editCheese.Category = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); context.SaveChanges(); return(Redirect("/Cheese")); } addCheeseViewModel.AddCategories(context.Categories.ToList()); return(View("Add", addCheeseViewModel)); }
public IActionResult Add(AddCheeseViewModel addCheeseViewModel) { if (ModelState.IsValid) { CheeseCategory newCheeseCategory = context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID); // Add the new cheese to my existing cheeses Cheese newCheese = new Cheese { Name = addCheeseViewModel.Name, Description = addCheeseViewModel.Description, Category = newCheeseCategory }; context.Cheeses.Add(newCheese); context.SaveChanges(); return(Redirect("/Cheese")); } addCheeseViewModel.AddCategories(context.Categories.ToList()); return(View(addCheeseViewModel)); }