// ############################################################ // Add new public VehicleBase AddNewVehicle(VehicleAdd newItem) { // Attempt to fetch the associated object var m = ds.Manufacturers.Find(newItem.ManufacturerId); if (m == null) { return(null); } else { // Associated object is valid, so continue // Create a new design model object var addedItem = Mapper.Map <Vehicle>(newItem); addedItem.Manufacturer = m; /* * var addedItem = new Vehicle() * { * Model = newItem.Model, * Trim = newItem.Trim, * ModelYear = newItem.ModelYear, * MSRP = newItem.MSRP, * Manufacturer = m * }; */ // Add and save ds.Vehicles.Add(addedItem); ds.SaveChanges(); // Prepare and return the result return(Mapper.Map <VehicleBase>(addedItem)); /* * // Prepare the return object * var v = new VehicleBase() * { * Id = addedItem.Id, * Model = addedItem.Model, * Trim = addedItem.Trim, * ModelYear = addedItem.ModelYear, * MSRP = addedItem.MSRP, * ManufacturerId = m.Id, * ManufacturerName = m.Name * }; * * // Return the result * return v; */ } }
// ############################################################ // Add new public VehicleBase AddNewVehicle(VehicleAdd newItem) { // Attempt to fetch the associated object var m = ds.Manufacturers.Find(newItem.ManufacturerId); if (m == null) { return null; } else { // Associated object is valid, so continue // Create a new design model object var addedItem = Mapper.Map<Vehicle>(newItem); addedItem.Manufacturer = m; /* var addedItem = new Vehicle() { Model = newItem.Model, Trim = newItem.Trim, ModelYear = newItem.ModelYear, MSRP = newItem.MSRP, Manufacturer = m }; */ // Add and save ds.Vehicles.Add(addedItem); ds.SaveChanges(); // Prepare and return the result return Mapper.Map<VehicleBase>(addedItem); /* // Prepare the return object var v = new VehicleBase() { Id = addedItem.Id, Model = addedItem.Model, Trim = addedItem.Trim, ModelYear = addedItem.ModelYear, MSRP = addedItem.MSRP, ManufacturerId = m.Id, ManufacturerName = m.Name }; // Return the result return v; */ } }
public ActionResult Create(VehicleAdd newItem) { if (ModelState.IsValid) { // Add the new object var addedItem = m.AddNewVehicle(newItem); if (addedItem == null) { return(RedirectToAction("index")); } else { return(RedirectToAction("details", new { Id = addedItem.Id })); } } else { // Uh oh... problems with the incoming data... // Prepare the data for the view (again) var addForm = Mapper.Map <VehicleAddForm>(newItem); // Add the 'select' UI control items addForm.Manufacturers = new SelectList(m.GetAllManufacturersAsList(), "Id", "Name", newItem.ManufacturerId); return(View(addForm)); /* * // Prepare the data for the view (again) * var addForm = new VehicleAddForm(); * * // Add the 'select' UI control items * addForm.Manufacturers = new SelectList(m.GetAllManufacturersAsList(), "Id", "Name", newItem.ManufacturerId); * * // Copy over the data that didn't validate * addForm.Model = newItem.Model; * addForm.Trim = newItem.Trim; * addForm.ModelYear = newItem.ModelYear; * addForm.MSRP = newItem.MSRP; * * return View(addForm); */ } }
public ActionResult Create(VehicleAdd newItem) { if (ModelState.IsValid) { // Add the new object var addedItem = m.AddNewVehicle(newItem); if (addedItem == null) { return RedirectToAction("index"); } else { return RedirectToAction("details", new { Id = addedItem.Id }); } } else { // Uh oh... problems with the incoming data... // Prepare the data for the view (again) var addForm = Mapper.Map<VehicleAddForm>(newItem); // Add the 'select' UI control items addForm.Manufacturers = new SelectList(m.GetAllManufacturersAsList(), "Id", "Name", newItem.ManufacturerId); return View(addForm); /* // Prepare the data for the view (again) var addForm = new VehicleAddForm(); // Add the 'select' UI control items addForm.Manufacturers = new SelectList(m.GetAllManufacturersAsList(), "Id", "Name", newItem.ManufacturerId); // Copy over the data that didn't validate addForm.Model = newItem.Model; addForm.Trim = newItem.Trim; addForm.ModelYear = newItem.ModelYear; addForm.MSRP = newItem.MSRP; return View(addForm); */ } }