public VehicleAdd ( |
||
newItem | ||
return |
public VehicleWithDetail VehicleAdd(VehicleAdd newItem)
{
// This method is called from the Vehicles controller...
// ...AND the Manufacturers controller
// When adding an object with a required to-one association,
// MUST fetch the associated object first
// Attempt to find the associated object
var a = ds.Manufacturers.Find(newItem.ManufacturerId);
if (a == null)
{
return null;
}
else
{
// Attempt to add the new item
var addedItem = ds.Vehicles.Add(Mapper.Map<Vehicle>(newItem));
// Set the associated item property
addedItem.Manufacturer = a;
ds.SaveChanges();
return (addedItem == null) ? null : Mapper.Map<VehicleWithDetail>(addedItem);
}
}
public ActionResult Create(VehicleAdd newItem) { // Validate the input if (!ModelState.IsValid) { return(View(newItem)); } // Process the input var addedItem = m.VehicleAdd(newItem); if (addedItem == null) { return(View(newItem)); } else { return(RedirectToAction("details", new { id = addedItem.Id })); } }