1 00:00:00,310 --> 00:00:02,360 While testing our ad entry form. 2 00:00:02,360 --> 00:00:06,640 We noticed that the entries controller isn't set up to process posts for 3 00:00:06,640 --> 00:00:08,773 the entries/add path. 4 00:00:08,773 --> 00:00:13,060 All MVC controllers inherit from the controller base class. 5 00:00:13,060 --> 00:00:19,040 Which provides a property named request that is of type HTTP request base. 6 00:00:19,040 --> 00:00:23,330 The request property provides us with information about the current request. 7 00:00:23,330 --> 00:00:25,630 Including the HTTP method. 8 00:00:25,630 --> 00:00:30,394 So, we can add some simple logic to check if the current request is a post. 9 00:00:30,394 --> 00:00:37,676 If and then Request.HttpMethod == and 10 00:00:37,676 --> 00:00:42,990 then POST, all in caps. 11 00:00:42,990 --> 00:00:46,080 And then let's add an else. 12 00:00:46,080 --> 00:00:49,960 While this approach would work, MVC gives us the tools that we need 13 00:00:49,960 --> 00:00:55,080 to define an action method for each HTTP verb that we need to support. 14 00:00:55,080 --> 00:00:57,800 We start by adding a second action method. 15 00:00:57,800 --> 00:00:59,527 I'll copy and paste the add method. 16 00:01:05,093 --> 00:01:09,850 A class can't have two methods with the same name and parameter signature. 17 00:01:09,850 --> 00:01:15,475 So I'll change the name of the new method to AddPost. 18 00:01:15,475 --> 00:01:20,081 But changing the name will keep MVC from being able to route Post to 19 00:01:20,081 --> 00:01:23,250 this method for the entries/add path. 20 00:01:23,250 --> 00:01:26,090 We can fix this by using an attribute. 21 00:01:26,090 --> 00:01:29,630 Attributes provide a way of associating information with our C# code. 22 00:01:30,750 --> 00:01:34,470 Instead of writing programming instructions using code statements, 23 00:01:34,470 --> 00:01:38,860 we can simply label or decorate our code with attributes. 24 00:01:38,860 --> 00:01:43,700 Attributes can be associated with classes, properties or methods. 25 00:01:43,700 --> 00:01:48,510 To associate an attribute with our method, we simply add a pair of brackets just 26 00:01:48,510 --> 00:01:53,370 above the method and type the name of the attribute, ActionName, 27 00:01:53,370 --> 00:01:58,820 followed by a set of parentheses if the attribute requires any parameter values. 28 00:01:58,820 --> 00:02:02,570 The ActionName attribute allows us to define the name of the action, 29 00:02:02,570 --> 00:02:07,080 past here as a parameter value that should be associated with this method. 30 00:02:07,080 --> 00:02:12,020 We could add another attribute, HttpPost to indicate that this method 31 00:02:12,020 --> 00:02:14,620 should only be associated with post requests. 32 00:02:14,620 --> 00:02:18,020 There are 2 ways that we could add multiple attributes. 33 00:02:18,020 --> 00:02:22,900 We can, stack them up, like this, or we can list 34 00:02:22,900 --> 00:02:28,330 multiple attributes inside a single set of brackets by separating them with commas. 35 00:02:28,330 --> 00:02:31,452 Let's test our new action method by putting a break point in 36 00:02:31,452 --> 00:02:33,398 each add method and running our app. 37 00:02:44,124 --> 00:02:47,470 Here's the Get request for the add entry page. 38 00:02:47,470 --> 00:02:52,010 Press F5 to continue, enter 1/1/2016 for 39 00:02:52,010 --> 00:02:55,420 the Date field value and click the Save button. 40 00:02:56,770 --> 00:02:59,260 And now we're in the add post method. 41 00:02:59,260 --> 00:03:01,520 Having separate action methods for Get and 42 00:03:01,520 --> 00:03:05,550 Post requests will help keep our code clean and more maintainable. 43 00:03:05,550 --> 00:03:06,910 Go ahead and stop the app. 44 00:03:09,210 --> 00:03:11,848 If you're using GitHub, let's commit our changes. 45 00:03:17,074 --> 00:03:20,541 Enter a commit message of, 46 00:03:20,541 --> 00:03:27,031 Added EntriesController Add POST action method. 47 00:03:27,031 --> 00:03:31,230 And click the Commit All button. 48 00:03:33,490 --> 00:03:38,058 Next, we'll see how we can use an NPC feature called Model binding to capture 49 00:03:38,058 --> 00:03:39,335 our forms post data.