public VehicleBase AddVehicle(VehicleAdd newItem) { // Calculate the next value for the identifier int newId = (vehicles.Count > 0) ? newId = vehicles.Max(id => id.Id) + 1 : 1; // Ensure that the new item's association property is value var associatedObject = manufacturers.SingleOrDefault(i => i.Id == newItem.ManufacturerId); if (associatedObject == null) { // Uh oh... return(null); } // Create a new item; notice the property mapping var addedItem = new Vehicle { Id = newId, Model = newItem.Model, Trim = newItem.Trim, ModelYear = newItem.ModelYear, MSRP = newItem.MSRP, // New... Manufacturer = associatedObject }; // Alternative... use AutoMapper to do the job... //addedItem = Mapper.Map<Vehicle>(newItem); //addedItem.Id = newId; //addedItem.Manufacturer = associatedObject; // Add the new item to the store vehicles.Add(addedItem); // Return the new item return(Mapper.Map <VehicleBase>(addedItem)); }
public ActionResult Create(VehicleAdd newItem) { // Handles HTTP POST, and adds the new item, then redirects to another view // Notice that this method is prefixed with an "attribute", [HttpPost] VehicleBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddVehicle(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(VehicleAdd newItem) { // Handles HTTP POST, and adds the new item, then redirects to another view // Notice that this method is prefixed with an "attribute", [HttpPost] VehicleBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddVehicle(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 }); }