This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let’s see how we can leverage MVC’s HTML helper methods to create our form.
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/v2.6 -b creating-forms-using-the-html-helpers
Using the Value
HTML Helper Method
As an alternative to using the TextBox
and TextArea
HTML helper methods to render our form fields, we could continue to manually write out the markup for the <input>
and <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>
The 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.
Additional Learning
For more information about the C# using
statement, see this page on the MSDN website.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up