Editor HTML Helper in ASP.NET Core MVC

Based on the data type we provided, the Editor HTML Helper in ASP.NET Core MVC used to produce HTML input components.

Editor HTML Helper in ASP.NET Core MVC
HTML Helper, Asp.Net Core, Programming, thetechfoyer, thetechfoyer.com

This Articles helps you to understand the concept of Editor HTML Helper in ASP.NET Core MVC



ASP.NET Core MVC supports following Data Types: -

  • string (input type text),
  • int,
  • decimal (input type text),
  • float, boolean (input type checkbox),
  • enum (input type text),
  • datetime (input type datetime).
// User Model
public class User
{
    public int _Id { get; set; }
    public string? Name { get; set; }
    public Gender Gender { get; set; }
    public int Age { get; set; }
    public bool IsNew { get; set; }
    public DateTime? DOB { get; set; }
}
public enum Gender
{
    Male,
    Female
}


// User Controller
public class UserController : Controller
{
    public ActionResult Index()
    {
        User usr = new User()
        {
            _Id = 101,
            Name = "Deepak",
            Gender = Gender.Male,
            Age = 44,
            IsNew = false,
            DOB = Convert.ToDateTime("29-02-1978")
        };
        return View(usr);
    }
}



Simple HTML helper Methods
We have listed the Employee model's property names.
Because of this, the Editor HTML Helper method generates the necessary input elements based on the datatype of the Employee model parameters.

@using Practice.Models
@model User
<table>
    <tr>
        <td>Idtd>
        <td>@Html.Editor("_Id")td>
    tr>
    <tr>
        <td>Nametd>
        <td>@Html.Editor("Name")td>
    tr>
    <tr>
        <td>Gendertd>
        <td>@Html.Editor("Gender")td>
    tr>
    <tr>
        <td>Agetd>
        <td>@Html.Editor("Age")td>
    tr>
    <tr>
        <td>IsNewtd>
        <td>@Html.Editor("IsNew")td>
    tr>
    <tr>
        <td>DOBtd>
        <td>@Html.Editor("DOB")td>
    tr>
table>



Strongly type HTML helper Methods
To specify the name, a lambda expression is required.

@using Practice.Models
@model User
<table>
    <tr>
        <td>Idtd>
        <td>@Html.EditorFor(emp => emp._Id)td>
    tr>
    <tr>
        <td>Nametd>
        <td>@Html.EditorFor(emp => emp.Name)td>
    tr>
    <tr>
        <td>Gendertd>
        <td>@Html.EditorFor(emp => emp.Gender)td>
    tr>
    <tr>
        <td>Agetd>
        <td>@Html.EditorFor(emp => emp.Age)td>
    tr>
    <tr>
        <td>IsNewtd>
        <td>@Html.EditorFor(emp => emp.IsNew)td>
    tr>
    <tr>
        <td>DOBtd>
        <td>@Html.EditorFor(emp => emp.DOB)td>
    tr>
table>

Whether you use the Editor() or the EditorFor() extension method, the outcome is the same.