Chapter 9: Creating Web Applications with ASP.NET MVC

281

Return RedirectToAction("Index")

Catch

Return View()

End Try

End Function

In the get Edit action method, you need to get a reference to the current record, indicated by the id being passed in, and pass that reference to the View for display. The post Edit action method accepts the modified customer and passes it to the repository for update in the database. You should also right-click in either of the Edit methods and select Add View. Make the View strongly typed, set the class to Customer, and the Content type to Edit.

The final operation to complete is discussed next, how to delete a Customer.

Deleting a Customer

The default template for creating a list added an ActionLink for Details, next to the Edit ActionLink. You can create a read-only details page if you want, or just ensure the list is in the format you want to show each customer record, but for our purposes the Details option is not necessary. So, this example replaces the Details link with one for deleting a record. Listing 9-12 shows the Delete Controller method, which replaces the Detail Controller method.

Listing 9-12 The Delete Controller method

C#:

//

// GET: /Customer/Delete/5

public ActionResult Delete(int id)

{

new CustomerRepository()

.DeleteCustomer(id);

TempData["Result"] = "Customer Deleted.";

return RedirectToAction("Index");

}

Page 304
Image 304
Microsoft 9GD00001 manual Deleting a Customer, 281, Listing 9-12 The Delete Controller method