Customize EditorFor or any XXXXFor

Whenever I use @Html.EditorForModel(), the one following the standard template is displayed in asp.net mvc

Default in Asp.net MVC Template

<div class="editor-label">
  <label for="[MY-PROPERTY]">MY-PROPERTYNAME</label>
</div>
<div class="editor-field">
  <input id="[MY-PROPERTYID]" name="MY-PROPERTYNAME" type="text" value=""> 
  <span class="field-validation-valid" data-valmsg-for="MY-PROPERTYNAME" data-valmsg-replace="true"></span>
</div>

I would like to customize the format of this HTML!

But attention !!!!!

  • I know this function → @Html.EditorForModel("MY-TEMPLATE")
  • I know this function → @Html.EditorFor(p => p.MY-PROPERTY, "MY-TEMPLATE")
  • I know this function → Views\{CONTROLLER}\EditorTemplates\{CLASSNAME\TYPE}
  • I know this function → Views\Shared\EditorTemplates\{CLASSNAME\TYPE}

My goal is to change the default template for the whole project!

No need to travel the entire project!

  • When used, @Html.EditorForModel()each property generates customized HTML !!

New template

The new template will look something like this:

<div class="control-group">
    @Html.LabelFor(p => p.PROPERTY, new { @class = "control-label" })
    <div class="controls">
        @Html.EditorFor(p => p.PROPERTY)
        <span class="help-inline"></span>
    </div>
</div>
+5
source share
3 answers

Object.cshtml Views\Shared\EditorTemplates\, @Html.EditorForModel() , (, classname.cshtml)

+2

ASP.NET MVC? , Microsoft ( ) 5 ?

(, ) - DisplayTemplates EditorTemplates , , .

. , , . , . - .

, .

0

I tried to do the same. Somehow to rewrite or override the methods of Editor, EditorFor or EditorForModel, because I wanted to change the HTML output altogether. I found this article that might help: http://www.headcrash.us/blog/2011/09/custom-display-and-editor-templates-with-asp-net-mvc-3-razor/

As @archil said. You can write your own object template, but the important thing is that it seems to be used in all cases. Even if you have a template for a certain type, this template is used as a master template.

0
source

All Articles