Input and Forms

In this lesson, we will learn how to use predefined tag helpers that can help coding forms and input fields.

The purpose of input tag helpers is to furnish an easy way for simultaneously copying an initial value and setting a convenient name to all input fields. Names of input fields are important. They determine where the value of the field is inserted once a form is posted to a controller action method. For instance, suppose that the target action method contains a parameter whose type is MyModel and that we want to pass the value of an input field to the NestedProperty1 property that is nested inside Property1 of MyModel. Then, we must give the name Property1.NestedProperty1 to that input field. In general, field names describe the path where to place the value of the submitted input in an instance of an action method parameter.

Since input field values are strings, they must be converted into the type of their target properties. We will discuss how this is done in the next chapter.

Razor has a predefined property type that accomplishes the simultaneous task of capturing both the path string of a property and its value. We will discuss it in detail in the next session.

asp-for and the ModelExpression type

In order to capture both the value and property path with a tag helper attribute, say asp-for, it is enough to define a property of type ModelExpression inside the tag helper definition, as shown in the example below:

Get hands-on with 1200+ tech skills courses.