6 Working with forms - tag helpers
This chapter covers
- Building forms with tag helpers
- Controlling input types with data annotations
- Populating select lists with server-side code
- Working with checkboxes and radio buttons in forms
- Uploading files to the server
The last chapter covered how model binding takes form input and binds it to handler parameters and public properties in Razor Pages. We learned that the key to ensuring successful binding is to ensure that the name of the binding source matches that of the binding target. Up to now, we have generated the name of our form controls manually. This potentially error-prone approach doesn’t move us much beyond fishing around in the Request.Form
collection for our posted values.
In Razor Pages applications, forms are where tag helpers shine. We have already seen their role in client- and server-side validation with the validation tag helpers. This chapter will explore how to generate forms to collect data from users and work with the model binding system. We will see how they can be used to ensure that control names work smoothly with binding target names. The following list details the tag helpers that are available for form building and the validation tag helpers.
- Form
- Form action
- Label
- Input
- Select an Option
- Textarea
Each tag helper targets the HTML element that it is named after, except the form action tag helper which targets buttons and input elements that have their type
attribute set to “submit” or “image”.