How are people using Editor/Display Templates vs. Html Helpers?
Asked Answered
M

3

7

Just wondering how and when people are using Editor/Display Templates vs. Html Helpers. Specifically I am talking about its use in rendering different UI control rather than rendering entities.

For instance, I have something like the following atm:

<tr>
    <th><%= Html.LabelFor(x => x.ActivityTypeId) %></th>
    <td><%= Html.EditorFor(x => x.ActivityTypeList, "MultiSelectDropDownList")%></td>
</tr>
<tr>
    <th><%= Html.LabelFor(x => x.Name) %></th>
    <td><%= Html.EditorFor(x => x.Name) %></td>
</tr>
<tr>
    <th><%= Html.LabelFor(x => x.Description) %></th>
    <td><%= Html.DisplayFor(x => x.Description, "DisplayString")%></td>
</tr>   

But of late I am wondering if I should be doing this:

<tr>
    <th><%= Html.LabelFor(x => x.ActivityTypeId) %></th>
    <td><%= Html.MultiSelectDropDownList(x => x.ActivityTypeList)%></td>
</tr>
<tr>
    <th><%= Html.LabelFor(x => x.Name) %></th>
    <td><%= Html.EditorFor(x => x.Name) %></td>
</tr>
<tr>
    <th><%= Html.LabelFor(x => x.Description) %></th>
    <td><%= Html.DisplayString(x => x.Description)%></td>
</tr>   

But if I go with this second option is there much point of using the middle editor for... I would be just a well off using Html.Textbox and have the benefit of being able to set any html property I like.

I'm interested what patterns people are using here... Any ideas?

Cheers Anthony

Mustache answered 4/2, 2010 at 22:26 Comment(0)
S
5

EditorFor and DisplayFor are the most powerful aspects of MVC 2 and in my opinion should be used and abused as much as possible.

Hop over to Brad Wilsons blog and check out how you can extend the Object templates to quickly whip out convention based screens from ViewModels decorated with attributes: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-5-master-page-templates.html

I'm using this technique in a current project and so far not one line of HTML has even been written for an individual screen. :D

Suppository answered 5/2, 2010 at 2:50 Comment(0)
U
0

I like so much the second one.

it's elegant and frees you from that buggous strings :)

Unfaithful answered 5/2, 2010 at 3:39 Comment(0)
P
0

I have modified (actually, in the process of modifying) the T4 Edit, Create, and View templates to spit out the code I want. That code does not use any DisplayFor or EditorFor methods. I haven't dug into the code for these methods, but I'm fairly certain you'll see some reflection going on in there. My modified templates presently generate TextBoxFor, DropDownListFor, and CheckBoxFor.

You could use the method in the Brad Wilson post that jfar mentioned if you prefer. At a minimum I would have the templates spit out code for each field using DisplayFor or EditorFor so you could go back later can change to a specific editor plus add any necessary attributes for the input field.

Phrasal answered 10/2, 2010 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.