人们如何使用编辑器/显示模板与 Html 助手?

发布于 2024-08-20 11:50:07 字数 1412 浏览 4 评论 0原文

只是想知道人们如何以及何时使用编辑器/显示模板与 Html 助手。具体来说,我谈论的是它在渲染不同 UI 控件而不是渲染实体中的用途。

例如,我有类似以下的 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 属性。

我对人们在这里使用的模式感兴趣......有什么想法吗?

干杯 安东尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-08-27 11:50:07

EditorFor 和 DisplayFor 是 MVC 2 最强大的方面,我认为应该尽可能多地使用和滥用。

跳转到 Brad Wilsons 博客,了解如何扩展对象模板,以从用属性装饰的 ViewModel 快速生成基于约定的屏幕:
http:// /bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-5-master-page-templates.html

我正在当前项目中使用此技术,但到目前为止还没有甚至已经为单个屏幕编写了一行 HTML。 :D

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

转身泪倾城 2024-08-27 11:50:07

我非常喜欢第二个。

它很优雅,可以让你摆脱那些有问题的字符串:)

I like so much the second one.

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

故事未完 2024-08-27 11:50:07

我修改了(实际上是修改过程中)T4 EditCreateView模板,吐出了我想要的代码。该代码不使用任何 DisplayForEditorFor 方法。我还没有深入研究这些方法的代码,但我相当确定您会看到其中发生的一些反射。我修改后的模板目前生成 TextBoxForDropDownListForCheckBoxFor

如果您愿意,可以使用 jfar 提到的 Brad Wilson 帖子中的方法。至少我会让模板使用 DisplayForEditorFor 为每个字段吐出代码,以便您稍后可以返回到特定编辑器并添加任何必要的属性输入字段。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文