Introduction:

 

In this article we will explain how to use input tag helper tag in asp.net core mvc with example.

 

Description:

 

The Input Tag Helper binds an HTML <input> element to a model expression in your razor view.

It’s Generates the id and name HTML attributes for the expression name specified in the asp-for attribute. asp-for="Property1.Property2" is equivalent to m => m.Property1.Property2. The name of the expression is what is used for the asp-for attribute value. And also it’s set the HTML type attribute value based on the model type and data annotation attributes applied to the model property

 

Example:

 

LoginModel.cs:

 

using System.ComponentModel.DataAnnotations;
 
namespace FormsTagHelper.ViewModels
{
    public class LoginModel
    {
        [Required]
        [Display(Name = " UserID ")]
        public string UserID { get; set; }
 
        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

Login.cshtml:

@model LoginModel


 
<form asp-controller="Account" asp-action="Login" method="post">
    UserID:  <input asp-for="UserID" /> <br />
    Password: <input asp-for="Password" /><br />
    <button type="submit">Login</button>
</form>

The code above generates the following HTML:

<form method="post" action="/Account/Login">
    UserID:
    <input type="text" data-val="true"
           data-val-required="The UserID field is required."
           id="UserID" name="UserID" value="" /> <br>
    Password:
    <input type="password" data-val="true"
           data-val-required="The Password field is required."
           id="Password" name="Password" /><br>
    <button type="submit">Login</button>
    <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>" />
</form>

I hope you understand now how to use input html helper in ASP.NET Core MVC using  Visual Studio 2017.

Please share your valuable feedback or comments to improve my future articles.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like