public ActionResult Edit(int?id, VehicleEdit newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.Id })); } if (id.GetValueOrDefault() != newItem.Id) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } // Attempt to do the update var editedItem = m.VehicleEditMSRP(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.Id })); } else { // Show the details view, which will have the updated data return(RedirectToAction("details", new { id = newItem.Id })); } }
public VehicleWithDetail VehicleEditMSRP(VehicleEdit newItem) { // Attempt to fetch the object // When editing an object with a required to-one association, // MUST fetch its associated object var o = ds.Vehicles.Include("Manufacturer") .SingleOrDefault(v => v.Id == newItem.Id); if (o == null) { // Problem - item was not found, so return return(null); } else { // Update the object with the incoming values ds.Entry(o).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return(mapper.Map <VehicleWithDetail>(o)); } }
public VehicleWithDetail VehicleEditMSRP(VehicleEdit newItem) { // Attempt to fetch the object // When editing an object with a required to-one association, // MUST fetch its associated object var o = ds.Vehicles.Include("Manufacturer") .SingleOrDefault(v => v.Id == newItem.Id); if (o == null) { // Problem - item was not found, so return return null; } else { // Update the object with the incoming values ds.Entry(o).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return Mapper.Map<VehicleWithDetail>(o); } }
public ActionResult Edit(int? id, VehicleEdit newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.Id }); } if (id.GetValueOrDefault() != newItem.Id) { // This appears to be data tampering, so redirect the user away return RedirectToAction("index"); } // Attempt to do the update var editedItem = m.VehicleEditMSRP(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.Id }); } else { // Show the details view, which will have the updated data return RedirectToAction("details", new { id = newItem.Id }); } }