admin管理员组文章数量:1392076
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) %>
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 plexity 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?
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) %>
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 plexity 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?
Share Improve this question edited Jan 27, 2010 at 5:19 Nathan Taylor asked Aug 20, 2009 at 19:21 Nathan TaylorNathan Taylor 24.6k20 gold badges101 silver badges158 bronze badges2 Answers
Reset to default 3ScottGu 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) %>
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.
Here is one example I found to work well.
Let's say, you have a customer that has an Address. You cannot create an Address for a NEW Customer, but via Association, you could have an object Customer that has a field Address.
Then, in your "Create" method for Customer you invoke Html.EditorFor(c => c.Address);
(and you can create custom template for your needs here) that will produce pletely populated Address object, which you can save before Customer, thus solving the dependency.
Now, when you have Reference Data, such as Country list or States, or whatever, it maybe better to just use Partial view to render it and not bother with Association.
Hope this helps,
-vlad
本文标签: javascriptASPNET MVC 2When To Use Templates vs When to Use Partial ViewsStack Overflow
版权声明:本文标题:javascript - ASP.NET MVC 2 - When To Use Templates vs When to Use Partial Views - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768252a2624176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论