public ActionResult Edit(MenuItem menuItem) { if (!ModelState.IsValid) { ViewBag.FoodTypes = new SelectList(_dbContext.FoodTypes.ToList(), "Id", "Name"); return View(menuItem); } MenuItem oldItem = _dbContext.MenuItems.FirstOrDefault(mi => mi.Id == menuItem.Id); if (oldItem == null) { ModelState.AddModelError("Summary", "The menu item for the given id was not found."); return View(menuItem); } int foodTypeId = 0; if (!int.TryParse(Request.Form["FoodType.Id"], out foodTypeId)) { ModelState.AddModelError("Summary", "The food type for the submitted menu item for the given id was not found."); return View(menuItem); } FoodType type = _dbContext.FoodTypes.First(ft => ft.Id == foodTypeId); oldItem.FoodType = type; oldItem.Name = menuItem.Name; oldItem.Description = menuItem.Description; oldItem.HalfOrderPrice = menuItem.HalfOrderPrice; oldItem.WholeOrderPrice = menuItem.WholeOrderPrice; _dbContext.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult Create(MenuItem menuItem) { if (!ModelState.IsValid) { ViewBag.FoodTypes = new SelectList(_dbContext.FoodTypes.ToList(), "Id", "Name"); return View(menuItem); } int foodTypeId = 0; if (!int.TryParse(Request.Form["FoodType.Id"], out foodTypeId)) { ViewBag.FoodTypes = new SelectList(_dbContext.FoodTypes.ToList(), "Id", "Name"); return View(menuItem); } FoodType type = _dbContext.FoodTypes.First(ft => ft.Id == foodTypeId); menuItem.FoodType = type; _dbContext.MenuItems.Add(menuItem); _dbContext.SaveChanges(); return RedirectToAction("Index"); }