/* * This is where new GrocertItem records are created (shocker) * The user is directed here if they click an actionlink in the index page for GroceryItems * the actionlink feeds this function the GroceryListId, so that the new groceryItem is given a * foreign key associating it with the appropriate list * * * This function redirects the user to the GroceryItemForm view, feeding it * a GroceryItemFormViewModel instance, which contains the listId, and will later * contain the GroceryItem. */ public ActionResult New(int GroceryListId) { GroceryItemFormViewModel m = new GroceryItemFormViewModel { GroceryListId = GroceryListId }; return(View("GroceryItemForm", m)); }
//See (New)'s documentation, similar except that this edits an existing record public ActionResult Edit(int id) { var groceryItem = groceryItemEngine.GetGroceryItem(id); if (groceryItem == null) { return(RedirectToAction("Error", "Home")); } var viewModel = new GroceryItemFormViewModel { GroceryItem = groceryItem, GroceryListId = groceryItem.GroceryListId }; return(View("GroceryItemForm", viewModel)); }
public ActionResult Save(GroceryItem groceryItem) { if (groceryItem.Title == null || groceryItem.Quantity == 0) { GroceryItemFormViewModel m = new GroceryItemFormViewModel { GroceryItem = groceryItem, GroceryListId = groceryItem.GroceryListId }; return(View("GroceryItemForm", m)); } if (groceryItem.GroceryItemId == 0) { groceryItemEngine.InsertGroceryItem(groceryItem); } else { var groceryItemsInDb = groceryItemEngine.GetGroceryItem(groceryItem.GroceryItemId); groceryItemEngine.UpdateGroceryItem(groceryItem.GroceryItemId, groceryItem); } return(RedirectToAction("Index", "GroceryItems", new { id = groceryItem.GroceryListId })); }