Exercise 5: Handling Deletion

The ability to delete albums is not yet implemented. This is what this exercise will be about. Like before, you will implement the Delete scenario using two separate methods within the StoreManagerController class:

  1. One action method will display a confirmation form
  2. A second action method will handle the form submission

Task 1 – Implementing the HTTP-GET Delete Action Method

In this task, you will implement the HTTP-GET version of the Delete action method to retrieve the album’s information.

  1. Start Microsoft Visual Web Developer 2010 Express from Start | All Programs | Microsoft Visual Studio 2010 Express | Microsoft Visual Web Developer 2010 Express.
  2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex05-HandlingDeletion\Begin, select MvcMusicStore.sln and click Open.
  3. Open StoreManagerController class. To do this, expand the Controllers folder and double-click StoreManagerController.[cs|vb].
  4. The Delete controller action is exactly the same as the previous Store Details controller action: it queries the album object from the database using the id provided in the URL and returns the appropiate View. To do this, replace the HTTP-GET Delete action method code with the following:

    (Code Snippet – ASP.NET MVC 3 Helpers and Forms and Validation – Ex5 Handling Deletion HTTP-GET Delete action – CSharp)

    C#

    //
    FakePre-0275179da27e4e3fbebad4c4b7359862-2e0f3b9d30544c168839a05c17d69251FakePre-379235fa6fea41648a53e53741073f4b-6e3b8c49507a4772a6b981fe4db1dcb7public ActionResult Delete(int id) { var album = storeDB.Albums.Single(a => a.AlbumId == id); return View(album); }FakePre-f7ff54d81ec3473993121dbd20024982-6a0970866ecf45d596d8b2e3a743a8ca

    (Code Snippet – ASP.NET MVC 3 Helpers and Forms and Validation – Ex5 Handling Deletion HTTP-GET Delete action – VB)

    Visual Basic

    '
    FakePre-1c2f7aa534904088a7c462e7ef394c12-260b51248943400a9f85bf0aa7e3e18aFakePre-628e2fdebfc34f3994818c812aadd22b-0e92b2c9cad64fd5b9e7f825b78670a6Function Delete(ByVal id As Integer) As ActionResult Dim album = storeDB.Albums.Single(Function(a) a.AlbumId = id) Return View(album) End FunctionFakePre-1d1579e6baeb4d25a4379b2b699618b7-1ed8a81786ae4d27ab6ae5c4b89b348a

  5. Before creating the new View template, you should build the project so that the Add View Dialog knows about the class to use. Build the project by selecting the Debug menu item and then Build MvcMusicStore.

    Figure 29

    Building the project

  6. Right-click inside the Delete action method and select Add View. This will bring up the Add View dialog.
  7. In the Add View dialog, verify that the View name is Delete. Check the Create a strongly-typed view checkbox and select Album (MvcMusicStore.Models) from the View data class drop-down. Select Delete from the View content drop-down. Leave the other fields with their default value and then click Add.

    Figure 30

    Adding a Delete View

  8. The Delete template shows all the fields from the model. You will show only the album’s title. To do this, replace the asp:Content with the following code:

    HTML(C#)

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    FakePre-211f7cd0afb34a1e9e5bb98756963cba-4bf34c0806c14588bf3b952e840e9016 <h2>Delete Confirmation</h2> <p> Are you sure you want to delete the album titles <strong><%: Model.Title %></strong> ? </p> <div> <% using (Html.BeginForm()) { %> <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List", "Index") %> <% } %> </div>FakePre-bd40cf9433bd418d991a162caf5221ef-8341d30ff4eb4c81b8b52d40cc00dec4FakePre-0e756205512c444a8552273fa422f28a-4c682b1b1a0c414fb95f029ef2a4ced7FakePre-fcfe50cd831b43c5b3609f608d1ec022-c23e836fd6da4256a0463de98f562ecbFakePre-bd0ecbc2ac7647ecb996db4c3ecc3e00-447b4969e6124298ac8d234ba54fde62

    HTML(Visual Basic)

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    FakePre-c2affab911d548fb85f894638bf5e11c-11a29843073e43f68976d4a030cf023f <h2>Delete Confirmation</h2> <p> Are you sure you want to delete the album titles <strong><%: Model.Title %></strong> ? </p> <div> <% using Html.BeginForm()%> <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List", "Index") %> <% End Using %> </div>FakePre-b0333de5dac245b28472a0688fbd8c75-b523b8dd3e3e46879e5ef7fb70df741cFakePre-d55473c3b0a04b6380d55eacffe01c5b-993c26347d114dcbb1d82acf355893c0FakePre-57fafc1a244a4943bd0363811854f008-1aca5df66f0547d492c103e17dbba613FakePre-81a96c441ae94d179bc62af94ab792f8-4c0d664b327f40bd98b685f02c5f3762

Task 2 – Running the Application

In this task, you will test that the StoreManagerDelete View page displays a confirmation deletion form.

  1. Press F5 to run the Application.
  2. The project starts in the Home page. Change the URL to /StoreManager. Select one album to delete by clicking Delete and verify that the new view is uploaded.

    Figure 31

    Deleting an Album

Task 3– Implementing the HTTP-POST Delete Action Method

In this task, you will implement the HTTP-POST version of the Delete action method that will be invoked when a user clicks the Delete button. The method should delete the album in the database.

  1. Close the browser if needed, to return to the Visual Studio window. Open StoreManagerController class. To do this, expand the Controllers folder and double-click StoreManagerController.[cs|vb].
  2. Replace HTTP-POST Delete action method code with the following:

    (Code Snippet – ASP.NET MVC 3 Helpers and Forms and Validation – Handling Deletion HTTP-POST Delete action – CSharp)

    C#

    //
    FakePre-405742b96cd9426cb803091f3f2b3c21-14cc5f5e7c0f41c296428f2304bcf537FakePre-e1b70f452e8c4e42ae3e70b50d310207-7abed2078a24453aa4c5a5a6e969450fFakePre-d780c9b4a770463e95478474a4d58298-d9c4718979ac48a8aeff62a271528050FakePre-87cfa83e59f24bf1ade286c0f039924c-b47afb15a5b448c999ae60ef3e38375bFakePre-624c6d34574343f0baec8b1c3a8412a1-d44ef59adbeb470b9433c72f0ad16d97 var album = storeDB.Albums .Include("OrderDetails").Include("Carts") .Single(a => a.AlbumId == id); storeDB.DeleteObject(album); storeDB.SaveChanges(); return RedirectToAction("Index");FakePre-e3510c01f86d4de282dfa07aeb1b3f1a-677c3201317c415c990af08b35727b91FakePre-bdbe8542328946e5b27ae02d62febb16-cd0d6e32d666456090316997bf3d3233FakePre-f18957aef3014a7c88bb62330f0c8edb-449bd253cb2340e087cf3b7d534fdda4FakePre-d3b80dd853a44f038959ba42d3b9b453-99595be1e2fe4e9990d6b97e6fa317a1

    (Code Snippet – ASP.NET MVC 3 Helpers and Forms and Validation – Handling Deletion HTTP-POST Delete action – VB)

    Visual Basic

    '
    FakePre-cd2218672aeb4e9b92f14c0eb193c88a-21fc480562d040dba017ae4e929f0b24FakePre-29d830815f824b739c3f79bf607e4d35-4b9db69179094f7ba4b97fdbbec54112FakePre-ad69dba0f10e447db4a657ee010708d1-7e8bdbaa70314b31a219991d3439b756FakePre-8527d9434070479e919f746f23632d3a-0930867bdfd64c9289bfc351fece6a4eFakePre-38cacbb27b4c430e923e5d87d4bf583b-a76c4e9c8ff44496a0b3d2366d65e25bFakePre-aa97c795ecf24ea8aa7ab521f7a68d53-bbb8bbc7f6b547f3adf70a2d598c11c2FakePre-f9ee1e7d7e914aeb8ffdb4110be5764c-5c46cfd6113a4bf1b55a9671b0a363d3 Dim album = storeDB.Albums.Include("OrderDetails").Include("Carts").Single( Function(a) a.AlbumId = id) storeDB.DeleteObject(album) storeDB.SaveChanges() Return RedirectToAction("Index")FakePre-0386acb81e7f457ca3b5a9a4961560e8-e4babde6c34144a599f7d5b580c354ffFakePre-de72c84bc4524a09bf0bf25bb55287fb-52a404fa09864e248ba94725f0933108FakePre-6809cde667154a1d8248be76165e1df8-18509c3071ae42d1b166c776657604c2FakePre-d2b5a06ccda74d9a8d69a5e04fe48b1b-e2c4e898ac094aa6b09d3540131e5742

Task 4 – Deleting on Cascade

Because of Referential Integrity, a deletion of an Album could raise an exception if it has OrderDetails entries. To solve this, you should allow cascade deletes. So, when you delete an Album, it will delete all the OrderDetails entries for that album. In this task, you will activate deletion on cascade.

Note:
In this scenario you will delete an Album no matter if it has an order associated. Consider that in other application scenarios this could not be the correct action.

  1. On the Solution Explorer expand the Models folder and then double-click StoreDB.edmx. This opens the Entity Data Model designer.
  2. Open the Models/StoreDB.edmx entity diagram. Right-click the relation between Album and OrderDetail and select Properties.

    Figure 32

    Editing Properties of a Relation

  3. In the Properties window, set the End1 OnDelete value to Cascade.

    Figure 33

    OnDelete property

Task 5 – Running the Application

In this task, you will test that the StoreManagerDelete View page lets you delete an Album and then redirects to the StoreManager Index View.

  1. Press F5 to run the Application.
  2. The project starts in the Home page. Change the URL to /StoreManager. Select one album to delete by clicking Delete. Confirm the deletion by clicking Delete button:

    Figure 34

    Deleting an Album

  3. Verify that the album was deleted since it does not appear in the Index.aspx page.

Next Step

Exercise 6: Adding Validation