public ActionResult Create(ModelIngredient ingredient) { try { if (ModelState.IsValid) { if (!Repo.IngredientNameExists(ingredient.Name)) { Repo.AddIngredient(new Ingredient { Name = ingredient.Name }); } else { ModelState.AddModelError("Create", "ingredient already exists"); return(View()); } } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public ActionResult Edit(int id, ModelIngredient ingredient) { try { ingredient.IngredientId = id; if (Repo.UpdateIngredient(new Ingredient { Name = ingredient.Name, IngredientId = ingredient.IngredientId })) { return(RedirectToAction(nameof(Index))); } //if get here then the ingredient wasn't found ModelState.AddModelError("Edit", "ingredient doesnt exist"); return(View()); } catch { return(View()); } }