public ActionResult Create(ProductAdd newItem) { // Model state validity check // If not valid, redirect to the index view // Otherwise, continue // Attempt to add the new item, using the manager method // If the attempt fails, redirect to the index view // Otherwise, redirect to the details view for the added object ProductBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddNewProduct(newItem); } else { // Return the object so the user can edit it correctly return(View(newItem)); } // If the incoming data is valid and the new data was added, redirect return(RedirectToAction("index")); }
public ActionResult Create(ProductAdd newItem) { // Model state validity check // If not valid, redirect to the index view if (!ModelState.IsValid) { return(RedirectToAction("index")); } else // Otherwise, continue { // Attempt to add the new item, using the manager method var addedItem = m.AddNewProduct(newItem); // If the attempt fails, redirect to the index view if (addedItem == null) { return(RedirectToAction("index")); } // Otherwise, redirect to the details view for the added object else { return(RedirectToAction("Details", new { id = addedItem.Id })); } } }