Updating Our Controller to Handle Deletes5:01 with James Churchill
As the first step in setting up our “Delete Entry” page, we’re ready to update our controller to handle deletes.
To follow along commiting your changes to this course, you'll need to fork the aspnet-fitness-frog repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-fitness-frog git checkout tags/v5.3 -b updating-our-controller-to-handle-deletes
We're almost done. 0:00 Let's see how to delete entries. 0:02 Here's our entries list page. 0:04 When we click on an entry's delete button, we're making a Git request for 0:06 our delete page. 0:10 As you can see, there's not much here right now. 0:11 We'll finish this page in this video. 0:14 Let's take a look at our controller's delete action method. 0:16 We can see here that it's currently ensuring that we have an ID 0:26 parameter value and then returning the view. 0:30 Should we update this action method to delete the specified entry? 0:33 No it's not a good idea for a Git request to change data. 0:37 Performing a Git request should always be a safe action to make. 0:40 Instead, our delete action method will return a view that will allow the user 0:45 to review the entry and confirm that they want to delete it clicking on that 0:50 views delete button will make a post request to the server to delete the entry. 0:54 Go ahead and stop the app like we did in an earlier video. 0:59 Let's think through the changes that we need to make to our controller and 1:03 add to do comments. 1:06 First we need to retrieve the entry for the provided ID parameter value. 1:08 To do, retrieved entry for 1:14 the provided ID parameter value. 1:18 And return not found if an entry wasn't found. 1:24 Return not found if an entry wasn't found, 1:28 then pass the entry to the view. 1:35 Pass the entry to the view. 1:45 We also need a delete post action method so lets stub that method out. 1:48 First, this method should only accept post request. 1:54 So start with the [HttpPost] attribute. 1:57 Then the signature for our method, public ActionResult 2:01 Delete(int id). 2:07 Notice that we're just accepting an integer parameter value 2:13 instead of an entry model instance. 2:16 To delete an entry we just need the ID, not the whole model. 2:19 Inside of the action method, we need to do two things. 2:23 One, delete the entry. 2:27 TODO Delete the entry. 2:30 And two, redirect the user to the entries list page. 2:33 TODO Redirect to the "Entries" list page. 2:38 Now let's implement our to dos. 2:46 Starting with our delete get action method, we need to retrieve the entry for 2:50 the provided ID parameter value. 2:54 We've seen that code before. 2:57 Entry, entry, equals entries 2:58 repository, get entry int id. 3:05 And then we need to return not found if an entry wasn't found. 3:11 We've seen that code before too. 3:15 If entry equals equals null 3:19 return HTTP not 3:22 found and lastly we need to pass the entry to the view. 3:28 That's easy to do too just pass the entry variable to our view method call. 3:34 Hey, we're on a roll. 3:42 Now for our delete post action method. 3:44 To delete the entry, we just need to call the repositories delete method. 3:51 EntriesRepository.DeleteEntry. 3:55 Then we need to redirect the user to the entries list page 4:04 by returning a call to the RedirectToAction method. 4:08 And that completes 4:11 the changes to our 4:16 controller. 4:21 Nice job, let's make sure that our project can successfully build 4:26 by pressing Ctrl+Shift+B. 4:29 And here in the output window, we can see that our project was successfully built. 4:32 If you're using GitHub, let's commit our changes. 4:37 Enter a commit message of updated the controller 4:44 to support the delete page and click the commit all button. 4:49 Now we need to work on the view which we'll do after the break. 4:58
You need to sign up for Treehouse in order to download course files.Sign up