How do people use Editor / Show Templates vs Html Helpers?

It’s just interesting how and when people use the Editor / Show templates against Html Helpers. In particular, I'm talking about its use in rendering various user interface controls, rather than rendering objects.

For example, 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 lately, I am wondering if I should do 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 move on to this second option, it makes a lot of sense to use the middle editor for ... I would just use Html.Textbox well and be able to set any html property that I like.

I wonder what patterns people use here ... Any ideas?

Cheers Anthony

+7
asp.net-mvc mvc-editor-templates
source share
3 answers

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

Let's move on to Brad Wilsons blog and see how you can expand object templates to quickly scream 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 use this technique in the current project, and so far not a single HTML line has been written for a separate screen .: D

+5
source share

I like the second one.

it is elegant and frees you from these buggy lines :)

0
source share

I changed (in fact, during the modification process) the T4 Edit , Create and View templates to spit out the code I want. This code does not use DisplayFor or EditorFor . I have not delved into the code for these methods, but I'm sure you will see some reflection in it. My modified templates now generate TextBoxFor , DropDownListFor and CheckBoxFor .

You can use this method in a post by Brad Wilson mentioned in jfar if you prefer. At a minimum, I would have templates spitting out code for each field using DisplayFor or EditorFor , so you can come back later to change it in a specific editor plus add the necessary attributes for the input field.

0
source share

All Articles