282Microsoft Visual Studio 2010: A Beginner’s Guide

VB:

'

' GET: /Customer/Delete/5

Function Delete(ByVal id As Integer) As ActionResult Dim custRep As New CustomerRepository custRep.DeleteCustomer(id)

TempData("Result") = "Customer Deleted."

Return RedirectToAction("Index")

End Function

Besides showing how to use the repository for performing the delete operation, there are a couple of new items in Listing 9-12 that you’ll need to know about: TempData and specifying a View. TempData is a special object for holding data for a single display of a View. So, when the View displays, it can read the current value of TempData, but that same value will not be available on the next View unless the Controller explicitly loads it again.

In all of the other calls to View, it was assumed that a View named after the Controller method would be returned, so it wasn’t necessary to specify the name of the View. However, we don’t have a delete View, so we specify Index as the View explicitly.

To accommodate the delete operation, Listing 9-13 shows the modifications on the Index.aspx View for Customers (located under \Views\Customer).

Listing 9-13 Deleting a Customer

C#:

... content removed

<h2>Index</h2>

<p>

<% if (TempData["Result"] != null) { %>

<label><%= Html.Encode(TempData["Result"].ToString() )%>

</label> <% } %>

</p>

<table>

Page 305
Image 305
Microsoft 9GD00001 manual Listing 9-13 Deleting a Customer