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