public ManufacturerBase AddManufacturer(ManufacturerAdd newItem) { // Calculate the next value for the identifier int newId = (manufacturers.Count > 0) ? newId = manufacturers.Max(id => id.Id) + 1 : 1; // Create a new item; notice the property mapping var addedItem = new Manufacturer { Id = newId, Name = newItem.Name, Country = newItem.Country, YearStarted = newItem.YearStarted }; // Alternative... use AutoMapper to do the job... addedItem = Mapper.Map <Manufacturer>(newItem); addedItem.Id = newId; // Add the new item to the store manufacturers.Add(addedItem); // Return the new item return(Mapper.Map <ManufacturerBase>(addedItem)); }
public ActionResult Create(ManufacturerAdd newItem) { // Handles HTTP POST, and adds the new item, then redirects to another view // Notice that this method is prefixed with an "attribute", [HttpPost] ManufacturerBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddManufacturer(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")); // Another alternative is to redirect to the "details" view, like this... //return RedirectToAction("details", new { id = addedItem.Id }); }
public ActionResult Create(ManufacturerAdd newItem) { // Handles HTTP POST, and adds the new item, then redirects to another view // Notice that this method is prefixed with an "attribute", [HttpPost] ManufacturerBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddManufacturer(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"); // Another alternative is to redirect to the "details" view, like this... //return RedirectToAction("details", new { id = addedItem.Id }); }