Handling a Form Post3:41 with James Churchill
Let’s see how to update our controller to handle a form post.
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/v1.5 -b handling-a-form-post
For more information about C# Attributes, see this page on MSDN.
While testing our ad entry form. 0:00 We noticed that the entries controller isn't set up to process posts for 0:02 the entries/add path. 0:06 All MVC controllers inherit from the controller base class. 0:08 Which provides a property named request that is of type HTTP request base. 0:13 The request property provides us with information about the current request. 0:19 Including the HTTP method. 0:23 So, we can add some simple logic to check if the current request is a post. 0:25 If and then Request.HttpMethod == and 0:30 then POST, all in caps. 0:37 And then let's add an else. 0:42 While this approach would work, MVC gives us the tools that we need 0:46 to define an action method for each HTTP verb that we need to support. 0:49 We start by adding a second action method. 0:55 I'll copy and paste the add method. 0:57 A class can't have two methods with the same name and parameter signature. 1:05 So I'll change the name of the new method to AddPost. 1:09 But changing the name will keep MVC from being able to route Post to 1:15 this method for the entries/add path. 1:20 We can fix this by using an attribute. 1:23 Attributes provide a way of associating information with our C# code. 1:26 Instead of writing programming instructions using code statements, 1:30 we can simply label or decorate our code with attributes. 1:34 Attributes can be associated with classes, properties or methods. 1:38 To associate an attribute with our method, we simply add a pair of brackets just 1:43 above the method and type the name of the attribute, ActionName, 1:48 followed by a set of parentheses if the attribute requires any parameter values. 1:53 The ActionName attribute allows us to define the name of the action, 1:58 past here as a parameter value that should be associated with this method. 2:02 We could add another attribute, HttpPost to indicate that this method 2:07 should only be associated with post requests. 2:12 There are 2 ways that we could add multiple attributes. 2:14 We can, stack them up, like this, or we can list 2:18 multiple attributes inside a single set of brackets by separating them with commas. 2:22 Let's test our new action method by putting a break point in 2:28 each add method and running our app. 2:31 Here's the Get request for the add entry page. 2:44 Press F5 to continue, enter 1/1/2016 for 2:47 the Date field value and click the Save button. 2:52 And now we're in the add post method. 2:56 Having separate action methods for Get and 2:59 Post requests will help keep our code clean and more maintainable. 3:01 Go ahead and stop the app. 3:05 If you're using GitHub, let's commit our changes. 3:09 Enter a commit message of, 3:17 Added EntriesController Add POST action method. 3:20 And click the Commit All button. 3:27 Next, we'll see how we can use an NPC feature called Model binding to capture 3:33 our forms post data. 3:38
You need to sign up for Treehouse in order to download course files.Sign up