Question

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

Was it helpful?

Solution

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

OTHER TIPS

I like so much the second one.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top