Introduction
In my previous article, I have explained about the tag helper. This article explains about working with forms and other HTML elements that are used with the form using tag helper. The HTML form provides a way to post the data back to the server.
Form Tag Helper
In my previous article, I have explained about the tag helper. This article explains about working with forms and other HTML elements that are used with the form using tag helper. The HTML form provides a way to post the data back to the server.
Form Tag Helper
Form Tag Helper generates the HTML form elements with action attribute value for MVC Controller action (named route). It is an alternative to HTML helpers Html.BeginForm and Html.BeginRouteForm. It generates hidden "Request Verification Token" to prevent the cross-site request forgery if the post method is decorated with ValidateAntiForgeryToken attribute. We can also supply route value parameter of post method using "asp-route-<parameter name>" attribute. Here, <pameter name> is added to the route value. This is same as routeValues parameter passed to Html.BeginForm and Html.BeginRouteForm.
Example
- <form asp-controller="Home" asp-action="Save" method="post">
- <!-- Input and Submit elements -->
- </form>
Generated HTML


The action attribute value is generated from the FORM tag helper attributes: asp-controller and asp-action. The Form Tag Helper also generates "Request Verification Token" (in the hidden field) to prevent cross-site request forgery.
Input Tag Helper
It binds an HTML input element to the Model expression in Razor View. It can generate the id and name attribute based on the expression specified in the asp-for attribute. It is capable to set HTML type attribute value based on the Model type and data annotation applied to the Model property. It never overwrites the type of an attribute if we have specified. It generates HTML5 base validation attributes from the data annotation applied to the model property. It provides strong typing with model. If we change the Model property name and do not change the tag helper, it throws an error.
It binds an HTML input element to the Model expression in Razor View. It can generate the id and name attribute based on the expression specified in the asp-for attribute. It is capable to set HTML type attribute value based on the Model type and data annotation applied to the Model property. It never overwrites the type of an attribute if we have specified. It generates HTML5 base validation attributes from the data annotation applied to the model property. It provides strong typing with model. If we change the Model property name and do not change the tag helper, it throws an error.
| .net Type | Input Type |
| string | text |
| bool | checkbox |
| datetime | datetime |
| byte | Number |
| int | Number |
| double | Number |
| single | Number |
It also considers the data annotation attributes of Model property to generate the type of input. The following table contains some examples of data annotation and type.
| Data Annotation attribute | Input type |
DataType(DataType.Password) | password |
| EmailAddress | |
| Uri | uri |
| HiddenInput | hidden |
| Phone | tel |
| [DataType(DataType.Date)] | date |
| [DataType(DataType.Time)] | time |
Example - Model Class
- using System.ComponentModel.DataAnnotations;
- namespace FormTagHelper.Models
- {
- public class UserViewModel
- {
- public int UserId { get; set; }
- public string UserName { get; set; }
- [EmailAddress]
- public string EmailAdress { get; set; }
- }
- }
HTML in cshtml(\Views\Home\index.cshtml)
- <div class="row">
- <form asp-controller="Home" asp-action="Save" method="post">
- <!-- Input and Submit elements -->
- UserId : <input asp-for="UserId" /><br />
- User Name:<input asp-for="UserName" /><br />
- Email Address: <input asp-for="EmailAdress" />
- </form>
- </div>
Generated HTML


In the preceding example, the data annotation applied to the email property generated the metadata on the Model and the input tag helper uses this metadata to generate HTML5 attribute.
The "asp-for" attribute is a ModelExpression and assignment (right-hand side) is lambda expression. It means, asp-for = "propertyName" is equvalent to o=>o.propertyName in the generated code that does not require prefix model. We can also use @ character to start an inline expression used with the tag helper.
- @{
- var myNumber = 5;
- }
- <input asp-for="@myNumber" />
Textarea Tag Helper
It is very similar to Input Tag Helper. It generates the name and id attribute based on the Model and data annotation attributes from the Model. It provides strong typing with Models. It is an alternative to Html.TextAreaFor.
It is very similar to Input Tag Helper. It generates the name and id attribute based on the Model and data annotation attributes from the Model. It provides strong typing with Models. It is an alternative to Html.TextAreaFor.
Example
- Address : <textarea asp-for="Address"></textarea>



Join the conversation! Your thoughts help the community grow.