2010-02-04 73 views
7

想知道如何以及何時人們使用編輯器/顯示模板與Html Helpers。具體來說,我在談論它在渲染不同的UI控件而不是渲染實體時的使用。人們如何使用編輯器/顯示模板與Html Helpers?

舉例來說,我有類似如下的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> 

但最近我想知道如果我應該這樣做:

<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> 

但是,如果我用這個第二個選項去是否有很多使用中間編輯器的要點......我只是使用Html.Textbox而已,並且能夠設置我喜歡的任何html屬性。

我感興趣的是什麼樣的人在這裏使用...任何想法?

乾杯 安東尼

回答

0

我非常喜歡第二個。

它的優雅和釋放您從buggous串:)

0

我已經修改了(實際上,在修改的過程中)的T4 EditCreate,並View模板吐出我想要的代碼。該代碼不使用任何DisplayForEditorFor方法。我還沒有挖掘出這些方法的代碼,但我相當肯定你會看到一些反射在那裏發生。我修改的模板目前生成TextBoxFor,DropDownListForCheckBoxFor

如果您願意,您可以使用jfar提到的Brad Wilson文章中的方法。至少我會讓模板爲每個字段使用DisplayForEditorFor吐出代碼,以便稍後返回時可以更改爲特定的編輯器,併爲輸入字段添加任何必需的屬性。