Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating Forms Using the HTML Helpers6:43 with James Churchill
Let’s see how we can leverage MVC’s HTML helper methods to create our form.
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/v2.6 -b creating-forms-using-the-html-helpers
Value HTML Helper Method
As an alternative to using the
TextArea HTML helper methods to render our form fields, we could continue to manually write out the markup for the
<textarea> elements and use the
Value HTML helper method to set the element values.
If we used this approach, the “Date” field’s
<input> element would look like this.
<input type="text" id="Date" name="Date" class="form-control" value="@Html.Value("Date")" />
And the “Notes” field’s
<textarea> element would look like this.
<textarea id="Notes" name="Notes" class="form-control" rows="14" cols="20"> @Html.Value("Notes") </textarea>
Value HTML helper method internally uses ModelState to ensure that the user’s provided values are properly preserved. While this approach works fine, in the video I opted to use the most commonly used approach, which is to render the entire element using an HTML helper method.
For more information about the C#
using statement, see this page on the MSDN website.
We're currently doing a lot of manual work to create our form and
to manage our form's post data.
Let's see how MVC could make our lives easier.
Here's our view for the Add Entry page.
To start with,
we can use MVC's begin form HTML helper method to render the form element.
Forms have both an opening and closing element, and
MVC gives us a convenient way to ensure that the closing element is rendered.
We just need to wrap the call to the begin form method in a using statement.
@using parentheses, followed by a set of curly braces.
Then, inside of the parentheses, html.BeginForm.
Then we can cut and
paste our form content in between our using statement's curly braces.
And remove our old form element.
Typically, the using statement is used with unmanaged resources
to ensure that they're properly released and cleaned up.
Used in this context, the using statement is being used to ensure that the closing
form element is rendered after all of the form's content is rendered.
See the teacher's notes for more information about the using statement.
Now let's work on the heart of our form, our form fields.
To render a label element, we can use the Label HTML helper method.
The Label method requires that we supply the name of the property
to render a label for.
For the date field, we just need to supply the string, Date.
Our label element also needs a CSS class.
So we'll use the second method, overload,
which allows us to pass in an anonymous object for the label's HTML attributes.
Remember, we need to prefix the class property with an @ symbol
as the word class is a reserved keyword in C#.
To render an input element of type text, we can use the TextBox HTML helper method.
The first parameter is the name of our field, which again is the string date.
The second parameter is the value for the field.
We're going to pass null,
which will cause the helper method to use model state to get the field value.
And lastly, we'll pass an anonymous object to set the CSS class to form-control.
Before we test our changes, let's go ahead and update the remaining fields.
I'll copy and paste the date field contents to the activity field.
Remove the key values.
Then copy and paste that code to the other fields, including the notes field.
Removing the key values before we replace the other fields reduces the amount of
typing that we need to do and helps ensure that we don't get sloppy and
forget the change of field name.
Now let's fill in the missing values.
For the Notes field, we can render the label like we did for the other fields.
But for the text area element, we need to use the TextArea HTML helper method.
Change the TextBox method name to TextArea,
And add the rows and columns parameter values just after the second parameter.
That leaves just the button element.
There's not an HTML helper method to render buttons,
so we'll just leave it as it is.
All in all, these changes really didn't remove much code, yet.
We've got one more change to make.
Now that we're using the MVC HTML helper methods to render our form elements,
we can remove code from the controller's Add method.
All of this code that we needed to flow the user-provided values back to our
form can be removed.
We can do this because the HTML helper methods
that we're using to render our form field input or text area elements
internally use ModelState to get the user's attempted values.
This is a huge time saver.
Now, let's test our updates by pressing F5 to run our app.
I'll enter a value for each field.
1/1/2016, 1, 23,
Low, False, and, This is my note.
And save the form.
Here at our break point in the Add method, we can see in the Locals window that all
of the method parameters have the correct values.
Let's continue by pressing F5.
We're back at our form and all of our values have been correctly redisplayed.
Go ahead and stop the app.
If you're using GitHub, let's commit our changes.
Enter a commit message of,
Updated the Add Entry view to use HTML helper methods.
And click the Commit All button.
Next, we'll take our controller and view one step further towards the ideal
approach by updating both to use our entry data model.
You need to sign up for Treehouse in order to download course files.Sign up