5

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>
ridermansb
  • 10,779
  • 24
  • 115
  • 226

3 Answers3

2

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

archil
  • 39,013
  • 7
  • 65
  • 82
0

Why rewrite ASP.NET MVC? Is it not too big of a task to recreate what Microsoft (with their mass production capacities) was creating for last 5 years?

I suggest (obviously far from being the only way to live) is to define as many DisplayTemplates and EditorTemplates as you like for types you need and be done with it.

Framework by definition is extensible. What you trying to do is to replace one of main poles in it. And I don't know if its even worth it. - Just an opinion.

Hope this helps.

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • I'm using the Bootstrap Twiiter that contains a different html template from Asp.net MVC. I'm not trying to change any framework much less rewrite it, I'm just looking for a way to avoid rework. If I'm have to create a EditorTemplates/DisplayTemplates for each class, it would be a lot of work! I believe there is a smarter way to perform this task. – ridermansb Aug 24 '12 at 20:51
  • I disagree when you say that this is one of the main poles of the framework! I'm trying to customize the layout generated by it, and not change its structure or anything! There are dozens of other features that can be considered Polo framework, ModelBinders, Routes and Filters are some examples that I think are much more important than this. – ridermansb Aug 24 '12 at 20:53
  • @RidermandeSousaBarbosa I kinda both agree and disagree: if after hours you cannot figure out at least a way to rewrite `EditorForModel`, could it mean its not doable? or could it mean nobody done it before? if latter is the case, after 5 years on the market doesn't it seems strange? hmmm. Another thing is - rewriting Editor/Display Templates being a lot of work, do you think rewriting of EditorForModel (if possible) is less? :) I guess lets wait for better answers. I keen to see if its even possible? – Display Name Aug 24 '12 at 22:08
0

I was trying to do exactly the same thing. Somehow rewrite or override the Editor, EditorFor or EditorForModel methods because I wanted to change the HTML output in general. I found this article that may 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 specific type, this template is being used as a "master" template.

Tacud
  • 609
  • 5
  • 16