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.
You need to sign up for Treehouse in order to download course files.Sign up