Editor HTML Helper in ASP.NET MVC Application

What is Editor HTML Helper in ASP.NET MVC?

The Editor HTML Helper in ASP.NET MVC is used to generate HTML input elements based on the model’s data type. It provides a flexible way to render form fields without specifying the exact HTML elements, allowing MVC to automatically determine the appropriate input type.

Let's learn how to utilize the Editor HTML Helper in an ASP.NET MVC application with an example. First, start by creating an empty ASP.NET MVC application. Next, add the following models inside the Models folder.


namespace HTML_HELPER.Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        [Display(Name = "Name")]
        public string EmployeeName { get; set; }    
        public string Gender { get; set; }
        public int Age { get; set; }
        public bool isNewlyEnrolled { get; set; }
        public DateTime? DoB { get; set; }
    }
}
                                        

Editor HTML Helper in ASP.NET MVC:

Let's learn how to utilize the Editor HTML Helper in an ASP.NET MVC application with an example. First, start by creating an empty ASP.NET MVC application. Next, add the following models inside the Models folder.

MvcHtmlString Editor(string propertyname)


Creating MVC 5 Controller:

Create a controller named EmployeeController, then insert the following code into it.


namespace HTML_HELPER.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            Employee emp = new Employee()
            {
                EmployeeId = 101,
                EmployeeName = "Amreen",
                Gender = "Female",
                Age = 22,
                isNewlyEnrolled = true,
                DoB =Convert.ToDateTime("02-02-2002")
            };
            return View(emp);
        }
    }
}
                                        

Creating Views:

Paste the following code into the “Index.cshtml” view.


@using HTML_HELPER.Models
@model Employee
<table>
    <tr>
    <td>EmployeeId</td>
    <td>@Html.Editor("EmployeeId")</td>
</tr>
    <tr>
    <td>EmployeeName</td>
    <td>@Html.Editor("EmployeeName")</td>
</tr>
    <tr>
    <td>Gender</td>
    <td>@Html.Editor("Gender")</td>
</tr>
    <tr>
    <td>Age</td>
    <td>@Html.Editor("Age")</td>
</tr>;
    <tr>
    <td>isNewlyEnrolled</td>
    <td>@Html.Editor("isNewlyEnrolled")</td>
</tr>
    <tr>
    <td>DoB</td>
    <td>@Html.Editor("DoB")</td>
</tr>
</table>
                                       

Now, execute the application, and it will display the output.