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”.

6.1 The form and form action tag helpers

 
 
 
 

6.2 Input and label tag helpers

 
 

6.2.1 Understanding the input types

 
 
 

6.2.2 Using data annotation attributes to control presentation

 
 

6.2.3 Formatting the rendered date or time value

 
 

6.2.4 Using the DisplayAttribute to control labels

 

6.3 The select tag helper

 
 
 
 

6.3.1 Creating Options

 
 

6.3.2 Binding Multiple Values

 

6.3.3 Working with OptGroups

 
 
 

6.3.4 Binding Enumerations

 
 
 

6.4 Checkboxes and Radio Controls in Razor

 
 
 
 

6.5 Uploading Files

 
 
 

6.6 Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest