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

查看:92
本文介绍了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)

在实际查看/ 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佣工时(例如,​​如果你有你的富模型酒吧对象,酒吧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").

更​​强大,例如,如果你有一个列表&LT; T&GT; 在您的视图模型的东西,你可以使用 @ Html.DisplayFor (型号=&GT; model.CollectionOfFoo)和MVC是足够聪明,看到这是一个收集并呈现出单一显示每个项目(而不是一个部分,这将需要一个明确的fo​​r循环)。

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和包含的模型值跨度)。使用 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] ,这将影响该模板被选择来产生。它们也通常用于数据模型(即在数据库中重新present行模型等)模型中的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)

在另一方面部分是视图为中心,你大多关注与选择正确的局部视图。视图不一定需要一个模型来正常工作。它可以只是有一个共同的组被整个网站重用标记。当然,很多时候,你要影响在这种情况下,你可能希望在适当的视图模型通过这部分的行为。

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 这也是在这里值得一提。你可以把它看作是一个更强大的版本的部分在执行一个控制器子操作,然后呈现一个视图(这通常是一个局部视图)。这很重要,因为孩子的动作可以执行不以局部视图属于额外的业务逻辑。例如,它可以重新present一个购物车组件。之所以用它是为了避免在您的应用程序中执行的每个控制器购物车有关的工作。

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 助手的局部视图。这真的取决于你的应用是什么,以及如何因素它鼓励最大code再利用,同时避免重复。

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天全站免登陆