views:

2299

answers:

2

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 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?

+2  A: 

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) %>

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.

Runeborg
I have read Mr. Gu's post and it explains that the EditorFor can be used both for individual fields and whole objects. I'm referring to the latter case in my scenario above.
Nathan Taylor
Oh okay. He states the purpose of that in the post as well: "By default it will loop over the public properties of the object and generate a <label>, <input/> element, and any appropriate validation message for each property it finds.". So it automatically generates input for all properties of the object. On the object you can set UIHints on the properties to use custom controls for specific properties. I'll update my answer a bit.
Runeborg
So is that to say I do not need to explicitly define a template when I call EditorFor(SomeObject) so long as I don't need to do anything special like handle custom fields?
Nathan Taylor
Yes, as far as I understand, it will recursively(?) parse the properties and add proper editors depending on the types of the properties .
Runeborg
A: 

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 completely 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

Vladimir Zelenko