1

How to override EditorFor layout? For example if i want to render some text before or after editor field.

k0lpak
  • 555
  • 4
  • 18

2 Answers2

0

Here is the code sample to add some text before and after EditorFor.

Step 1: Define field in model class with a specific DataType with a logical sounding custom names like SmallText, CustomName etc. I just used "MyTypeOfField" in this example.

[Required(ErrorMessage = "Field_Value code is a required field.")]
    [MaxLength(100)]
    [Display(Name = "Field Display Name")]
    [DataType("MyTypeOfField")]
    public string MyField { get; set; }

Step 2: Create "MyTypeOfField.cshtml" in folder : Views/Shared/EditorTemplates with below code. If EditorTemplates folder is not present, create it.

@inherits System.Web.Mvc.WebViewPage<string>
@string.Format("{0, -10}","XXXXXXXXXX") @Html.TextBox("", "", new { style = "width:30px; text-align:right;" }) @string.Format("{0, -10}","1234567890")

Hope this helps!

Mayank
  • 334
  • 2
  • 11
-1

EditorFor is an extension method defined on HtmlHelper class. if you want to override it you have to subclass HtmlHelper. according to this discussion overriding extension methods is a bad idea. Even if you decide to go this way how would you make mvc use subclassed HtmlHelper instead of default one. your best bet could be overloading EditorFor method and using it in views instead of default ones.

Community
  • 1
  • 1
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155