Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize EditorFor or any XXXXFor

Whenever I use @Html.EditorForModel() 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!!!!!

  1. I know this feature -> @Html.EditorForModel("MY-TEMPLATE")
  2. I know this feature -> @Html.EditorFor(p => p.MY-PROPERTY, "MY-TEMPLATE")
  3. I know this feature -> Views\{CONTROLLER}\EditorTemplates\{CLASSNAME\TYPE}
  4. I know this feature -> Views\Shared\EditorTemplates\{CLASSNAME\TYPE}

My goal is to modify the default template for the entire project!

Without having to travel the entire project for this!

  • When using @Html.EditorForModel(), each property will generate a HTML customized!!

New template

The new template would be 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>
like image 912
ridermansb Avatar asked Nov 04 '22 17:11

ridermansb


1 Answers

You could create Object.cshtml template to your Views\Shared\EditorTemplates\ folder and @Html.EditorForModel() will use that template if it cannot find more specific one (for example template named classname.cshtml)

You could further read Custom Object Templates by Brad Wilson

like image 99
archil Avatar answered Nov 11 '22 21:11

archil