limitations of using @Html.EditorForModel limitations of using @Html.EditorForModel asp.net asp.net

limitations of using @Html.EditorForModel


EditorForModel default template is not highly customizable. You should write an EditorTemplate to solve all your problems. You can check this tutorial.You will have to write all the properties manually but you will do it only once for each model. Put the template in the folder EditorTemplates/ and your whole app can use it. To answer your bullets:

  1. Now you write your own template, you can use any type of input for your properties. The default editor for string type is a textbox (dropdown for enum type).

  2. You will have to write your own layout in the template. That is exactly what you want.

  3. You can also write an editor template for RESOURCECUSTOMFIELD and use it in your template. You can write a template even for IEnumerable<RESOURCECUSTOMFIELD> :

    @model IEnumerable<RESOURCECUSTOMFIELD>@foreach(var customModel in Model){    @Html.LabelFor(m => customModel.CUSTOMLABEL )    @Html.TextBoxFor(m => customModel.CUSTOMVALUE )}

And use it like (in your main template):

   @Html.EditorFor(m => m.RESOURCECUSTOMFIELD )

There is also something called DisplayTemplate if you want to change how the DisplayFor works for the model.


1.You can use [UIHint("custom_template")] above your model field.For example:

public class Class1    {        public int id { get; set; }        public string Name { get; set; }        public string LastName { get; set; }        [UIHint("gender_template")]        public string Gender { get; set; }    }

Then you should create gender_template.cshtml in Shared/EditorTemplates like the picture below.

enter image description here

example code in file gender_template.cshtml.

@Html.DropDownList(    "",    new SelectList(        new[]         {             new { Value = "Male", Text = "Male" },            new { Value = "Female", Text = "Female" },        },        "Value",        "Text",        Model    ))

Then in the View page code is

@model MvcApplication2.Models.Class1 @{    ViewBag.Title = "Index";}<h2>Index</h2>@Html.EditorForModel()

The result after run view page is

enter image description here

You can make more custom template as you need.

  1. You can do like @html.EditorFor, To use @Html.DisplayFor syntax you should create template file in Shared/DisplayTemplates, You can write/add your own html syntax in the template file.