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




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






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


@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>

The code above generates the following HTML:

<form method="post" action="/Account/Login">
    <input type="text" data-val="true"
           data-val-required="The UserID field is required."
           id="UserID" name="UserID" value="" /> <br>
    <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>" />

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.

Leave a comment

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

You may also like