public ActionResult Edit(int id, VehicleBase newItem) { // Two tests are required... if (ModelState.IsValid & id == newItem.Id) { // Attempt to update the item VehicleBase editedItem = m.EditVehicle(newItem); if (editedItem == null) { // There was a problem updating the object return(View(newItem)); } else { // Succesful - item was edited TempData["statusMessage"] = "Edits have been saved."; return(RedirectToAction("details", new { id = editedItem.Id })); } } else { // Return the object so the user can edit it correctly return(View(newItem)); } }
// ############################################################ // // GET: /Vehicles/Edit/5 public ActionResult Edit(int id) { // Prepare the object to be edited // We will be able to use the VehicleBase view model class // Then we'll selectively edit the scaffolded view to suit our needs // Attempt to get the object with the matching identifier VehicleBase fetchedObject = m.GetVehicleById(id); if (fetchedObject == null) { return(RedirectToAction("index")); } else { return(View(fetchedObject)); } }
// ############################################################ // // GET: /Vehicles/Delete/5 public ActionResult Delete(int?id) { // The type of the parameter above was changed to int?, a nullable int // This will suppress an error message // Attempt to fetch the object to be deleted // Notice that we must use a method on the nullable int VehicleBase itemToDelete = m.GetVehicleById(id.GetValueOrDefault()); if (itemToDelete == null) { return(RedirectToAction("index")); } else { return(View(itemToDelete)); } }
// ############################################################ // Edit existing item public VehicleBase EditVehicle(VehicleBase newItem) { // Attempt to fetch the object with the matching identifier var fetchedObject = ds.Vehicles.Find(newItem.Id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return(null); } else { // Update the object with the incoming values // Before doing this, we may have to perform // business-rule validations ds.Entry(fetchedObject).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return(Mapper.Map <VehicleBase>(fetchedObject)); } }
// ############################################################ // Edit existing item public VehicleBase EditVehicle(VehicleBase newItem) { // Attempt to fetch the object with the matching identifier var fetchedObject = ds.Vehicles.Find(newItem.Id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return null; } else { // Update the object with the incoming values // Before doing this, we may have to perform // business-rule validations ds.Entry(fetchedObject).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return Mapper.Map<VehicleBase>(fetchedObject); } }
public ActionResult Edit(int id, VehicleBase newItem) { // Two tests are required... if (ModelState.IsValid & id == newItem.Id) { // Attempt to update the item VehicleBase editedItem = m.EditVehicle(newItem); if (editedItem == null) { // There was a problem updating the object return View(newItem); } else { // Succesful - item was edited TempData["statusMessage"] = "Edits have been saved."; return RedirectToAction("details", new { id = editedItem.Id }); } } else { // Return the object so the user can edit it correctly return View(newItem); } }