ASP.NET MVC 2 - 当使用模板VS何时使用局部视图 [英] ASP.NET MVC 2 - When To Use Templates vs When to Use Partial Views

查看:138
本文介绍了ASP.NET MVC 2 - 当使用模板VS何时使用局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个在ASP.NET MVC 2 preVIEW 1的新功能是编辑模板的概念,并显示模板,让你可以支持pre-定义一个给定的对象将如何呈现显示或编辑用一个简单的HTML辅助电话:

One of the new features in ASP.NET MVC 2 Preview 1 is support for the concept of Editor Templates and Display Templates which allow you to pre-define how a given object will be rendered for display or editing with a simple HTML helper call:

<%=Html.EditorFor(customer => customer) %>
<%=Html.DisplayFor(customer => customer) %>

这是pretty很酷,但我实在不明白这和部分观点有异曲同工之妙的区别​​。此外,在例子,我看到编辑模板不包含实际的形式标记,并在我需要提供一些客户端的功能,以给定的编辑器的情况下(通过jQuery的说),我不能安全地方,$ C模板中的$ C,因为我不会有我在客户端添加逻辑窗体上的静态句柄。在我上我使用的应用程序有我使编辑内容编辑模板和部分景色的混合物。根据形式的复杂性,我创建一个编辑器,我选择了另一种方法,但本课程的增加不一致的应用程序不理想的水平。

This is pretty cool, but I don't really see the difference between this and a Partial View that serves the same purpose. Furthermore, in the examples I saw the Editor Templates do not contain the actual form tags and in the event that I need to provide some client-side functionality to a given editor (say through jQuery), I can't safely place that code in the template because I won't have a static handle on the form I am adding logic to in the client. In the application I am working on I have a mixture of Editor Templates and Partial Views which I render to edit content. Depending on the complexity of the form I am creating an editor for I've chosen one approach over the other, but this of course adds an undesirable level of inconsistency to the application.

为什么要使用模板上的局部视图,反之亦然?此外,使用编辑模板什么是客户端逻辑添加到编辑器而不复制它到使用该编辑每个视图的理想方法?当

Why use a Template over a Partial View or vise versa? Additionally, when using an Editor Template what is the ideal way to add client-side logic to the editor without copying it into every view that uses that editor?

推荐答案

ScottGu解释了他的博文约MVC V2

ScottGu explains some of this in his blogpost about MVC V2.

从我收集这会为每个传入到助手对象的属性创建投入。所以,如果你有对象:

From what I gather this will create inputs for each of the properties of the object you pass to the helper. So if you have the object:

public class Customer
{
    public string Name { get; set; }
    [UIHint("MyCoolCalendar")]
    public DateTime CoolDate { get; set; }
}

然后创建一个编辑器:

And then create an editor:

<%= Html.EditorFor(customer => customer) %>

这将产生对客户的名称的文本输入,以及一个CoolDate MyCoolCalendar(这是一个customdefined控制),您无需编写自定义的控制来包装整个对象。它会自动推导从属性的类型/ uihint控制的类型。至少,这是因为我已经明白它,而无需时间来测试它呢。

It will produce a text input for the name of the customer, and a MyCoolCalendar (which is a customdefined control) for CoolDate without you having to write a custom control to wrap the entire object. It automatically deduces the type of control from the type/uihint of the property. At least this is as I have understood it without having time to test it out yet.

这篇关于ASP.NET MVC 2 - 当使用模板VS何时使用局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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