ASP.NET MVC 3 - 部分模板 vs 显示模板 vs 编辑器模板 [英] ASP.NET MVC 3 - Partial vs Display Template vs Editor Template

查看:27
本文介绍了ASP.NET MVC 3 - 部分模板 vs 显示模板 vs 编辑器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,标题应该不言自明.

So, the title should speak for itself.

要在 ASP.NET MVC 中创建可重用的组件,我们有 3 个选项(可能是我没有提到的其他选项):

To create re-usable components in ASP.NET MVC, we have 3 options (could be others i haven't mentioned):

部分视图:

@Html.Partial(Model.Foo, "SomePartial")

自定义编辑器模板:

@Html.EditorFor(model => model.Foo)

自定义显示模板:

@Html.DisplayFor(model => model.Foo)

就实际的 View/HTML 而言,所有三个实现都是相同的:

In terms of the actual View/HTML, all three implementations are identical:

@model WebApplications.Models.FooObject

<!-- Bunch of HTML -->

那么,我的问题是 - 您何时/如何决定使用三者中的哪一个?

我真正想要的是在创建一个问题之前要问自己的问题列表,这些问题的答案可用于决定使用哪个模板.

What i'm really looking for is a list of questions to ask yourself before creating one, for which the answers can be used to decide on which template to use.

这是我发现 EditorFor/DisplayFor 更好的两件事:

Here's the 2 things i have found better with EditorFor/DisplayFor:

  1. 它们在呈现 HTML 帮助程序时尊重模型层次结构(例如,如果您的Foo"模型上有一个Bar"对象,则Bar"的 HTML 元素将使用Foo.Bar.ElementName"呈现,而部分将具有ElementName").

  1. They respect model hierarchies when rendering HTML helpers (e.g if you have a "Bar" object on your "Foo" model, the HTML elements for "Bar" will be rendered with "Foo.Bar.ElementName", whilst a partial will have "ElementName").

更健壮,例如,如果您的 ViewModel 中有一个 List,则可以使用 @Html.DisplayFor(model => model.CollectionOfFoo),并且 MVC 足够聪明,可以看到它是一个集合并为每个项目呈现单个显示(而不是 Partial,它需要显式的 for 循环).

More robust, e.g if you had a List<T> of something in your ViewModel, you could use @Html.DisplayFor(model => model.CollectionOfFoo), and MVC is smart enough to see it's a collection and render out the single display for each item (as opposed to a Partial, which would require an explicit for loop).

我也听说 DisplayFor 呈现一个只读"模板,但我不明白 - 我不能在那里扔一个表格吗?

I've also heard DisplayFor renders a "read-only" template, but i don't understand that - couldn't i throw a form on there?

谁能告诉我一些其他的原因?有没有比较这三者的列表/文章?

Can someone tell me some other reasons? Is there a list/article somewhere comparing the three?

推荐答案

EditorFor vs DisplayFor 很简单.这些方法的语义是生成编辑/插入和显示/只读视图(分别).在显示数据时使用 DisplayFor(即当您生成包含模型值的 div 和 span 时).在编辑/插入数据时使用 EditorFor(即在表单中生成输入标签时).

EditorFor vs DisplayFor is simple. The semantics of the methods is to generate edit/insert and display/read only views (respectively). Use DisplayFor when displaying data (i.e. when you generate divs and spans that contain the model values). Use EditorFor when editing/inserting data (i.e. when you generate input tags inside a form).

以上方法是以模型为中心的.这意味着他们将考虑模型元数据(例如,您可以使用 [UIHintAttribute][DisplayAttribute] 注释模型类,这将影响选择哪个模板为模型生成 UI.它们通常也用于数据模型(即代表数据库中行的模型等)

The above methods are model-centric. This means that they will take the model metadata into account (for example you could annotate your model class with [UIHintAttribute] or [DisplayAttribute] and this would influence which template gets chosen to generate the UI for the model. They are also usually used for data models (i.e. models that represent rows in a database, etc)

另一方面,Partial 是以视图为中心的,因为您最关心的是选择正确的局部视图.视图不一定需要模型才能正常运行.它可以只有一组通用的标记,可以在整个站点中重复使用.当然,很多时候你想要影响这个部分的行为,在这种情况下你可能想要传递一个合适的视图模型.

On the other hand Partial is view-centric in that you are mostly concerned with choosing the correct partial view. The view doesn't necessarily need a model to function correctly. It can just have a common set of markup that gets reused throughout the site. Of course often times you want to affect the behavior of this partial in which case you might want to pass in an appropriate view model.

你没有问到 @Html.Action ,这里也值得一提.您可以将其视为 Partial 的更强大版本,因为它执行控制器子操作,然后呈现视图(通常是局部视图).这很重要,因为子操作可以执行不属于局部视图的附加业务逻辑.例如,它可以代表一个购物车组件.使用它的原因是为了避免在应用程序中的每个控制器中执行与购物车相关的工作.

You did not ask about @Html.Action which also deserves a mention here. You could think of it as a more powerful version of Partial in that it executes a controller child action and then renders a view (which is usually a partial view). This is important because the child action can execute additional business logic that does not belong in a partial view. For example it could represent a shopping cart component. The reason to use it is to avoid performing the shopping cart-related work in every controller in your application.

最终的选择取决于您在应用程序中建模的是什么.还请记住,您可以混合搭配.例如,您可以有一个调用 EditorFor 助手的局部视图.这实际上取决于您的应用程序是什么以及如何将其分解以鼓励最大程度的代码重用同时避免重复.

Ultimately the choice depends on what is it that you are modelling in your application. Also remember that you can mix and match. For example you could have a partial view that calls the EditorFor helper. It really depends on what your application is and how to factor it to encourage maximum code reuse while avoiding repetition.

这篇关于ASP.NET MVC 3 - 部分模板 vs 显示模板 vs 编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆