Html.DisplayFor Template Helper in ASP.NET MVC 2.0 to render Complex Object

Working on a series of articles on ASP.NET MVC 2.0 that are subject to change when ASP.NET MVC 2.0 gets released as a part of Visual Studio 2010:

Although the names may change, I think the concepts will remain the same.

 

Html.DisplayFor Template Helper

I need to back up a bit and talk about the Html.DisplayFor Template Helper that has worked its way into ASP.NET MVC 2.0 Preview 1. If we look at the Contact Entity that we have been playing with thus far:

 

internal class Contact_Metadata

{

    [Required(ErrorMessage = "Name is required.")]

    [StringLength(100, ErrorMessage = "Name must be 100 characters or less.")]

    public string Name { get; set; }

 

    [Required(ErrorMessage = "Email is required.")]

    [StringLength(200, ErrorMessage = "Email must be 200 characters or less.")]

    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")]

    [DataType(DataType.EmailAddress)]

    public string Email { get; set; }

}

 

and use the Html.DisplayFor Helper in an ASP.NET MVC 2.0 View to display the Contact ( which is the strongly-typed Model for the View ) as such:

 

<%= Html.DisplayFor(contact => contact) %>

 

we get the following displayed in the default Details View:

 

ASP.NET MVC 2.0

 

Basically the template helpers did a little reflection on the Contact instance, outputting the names of the properties as well as their values. The only reason the email address is a mailto hyperlink is because we decorated it with a DataTypeAttribute of [DataType(DataType.EmailAddress)] which caused the Html.DisplayFor Template Helper to render it as a mailto hyperlink.

 

[DataType(DataType.EmailAddress)]

public string Email { get; set; }

 

You can go ahead and customize the output of the Contact Entity by including a folder, called DisplayTemplates, in your ASP.NET MVC 2.0 Website and adding a partial view with the same name as the type, Contact:

 

ASP.NET MVC 2.0 Template Helpers

 

I stuck it in the Shared Folder, but you can also put it in the Contacts Folder if you don't want it shared across all controllers. I created a basic partial view as such:

 

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ContactManager.Models.Contact>" %>
<p>
    <%= Html.LabelFor(c => c.Name) %>:
    <%= Html.DisplayFor(c => c.Name) %>
</p>
<p>
    <%= Html.LabelFor(c => c.Email) %>:
    <%= Html.DisplayFor(c => c.Email) %>
</p>

 

which caused Html.DisplayFor to use my Contact.ascx partial view as the template and output the Contact as such:

 

ASP.NET MVC 2.0

 

If you think this feels like ASP.NET Dynamic Data, you are right. By the time ASP.NET MVC 2.0 gets fully cooked in Visual Studio 2010 it will get even cooler than this. If you want to see this in action, make sure you check out this ASP.NET MVC 2.0 Preview 1 Screencast.

Hope this helps. ASP.NET MVC is getting really interesting!

David Hayden

 

ASP.NET MVC Book Reviews

 

posted on Thursday, August 13, 2009 7:24 PM

Main

David Hayden Google +

David Hayden Twitter

Health & Fitness

JavaScript Patterns Book Review

HTML 5 and CSS3 - Develop with Tomorrow's Standards Today

Professional ASP.NET Design Patterns Book Review

Beginning Mac Programming Book Review

C# in Depth Book Review

ASP.NET MVC

Orchard CMS

Categories