public ActionResult Create(EmployeeAdd newItem) { // Attention 20 - Notice the pattern for handling incoming data... // First, ensure that the incoming data is valid // Next, attempt to process the incoming data (add employee etc.) // Finally, if successful, redirect to another view // This is known as the PRG pattern - Post, Redirect, Get // Validate the input if (!ModelState.IsValid) { // Uh oh, problem with the data, show the form again, with the data return(View(newItem)); } // Process the input var addedItem = m.EmployeeAdd(newItem); if (addedItem == null) { // Uh oh, some problem adding, show the empty form again return(View(newItem)); } else { return(RedirectToAction("details", new { id = addedItem.EmployeeId })); } }
// Attention 13 - Add new customer public EmployeeBase EmployeeAdd(EmployeeAdd newItem) { // Attempt to add the new item // Notice how we map the incoming data to the design model object var addedItem = ds.Employees.Add(Mapper.Map <EmployeeAdd, Employee>(newItem)); ds.SaveChanges(); // If successful, return the added item, mapped to a view model object return((addedItem == null) ? null : Mapper.Map <Employee, EmployeeBase>(addedItem)); }