DataTypeAttribute and ASP.NET MVC 2.0 Goodness with Templated Helpers

I can't tell you how excited I am about templated helpers in ASP.NET MVC 2.0. Templated Helpers are subject to change and naming when ASP.NET MVC 2.0 is officially released as a part of Visual Studio 2010, but in ASP.NET MVC 2.0 Preview 1 they are roughly the Html.LabelFor, Html.DisplayFor and Html.EditFor extension methods on HtmlHelper that work in concert with System.ComponentModel.DataAnnotations.

There is a lot of goodness and potential here and I showed you a tiny bit in my ASP.NET MVC 2.0 tutorial that showed validation in the DefaultModelBinder that works with the ValidationAttributes in System.ComponentModel.DataAnnotations:

I also showed you a bit when showing Buddy Classes for holding Metadata in ASP.NET MVC 2.0:

Now let's explore another area that is more display oriented: the DataTypeAttribute in System.ComponentModel.DataAnnotations.

 

DataTypeAttribute in System.ComponentModel.DataAnnotations

The DataTypeAttribute is used to help specify exactly what the data represents in a database column / class property. For example, you can have a string property ( and varchar database column ), called Email, that holds an email address. You decorate the Email Property with a DataTypeAttribute specifying this metadata and the underlying framework can do some coolness with it in terms of validation, display, etc.

For example, let's take my previous tutorial that uses a buddy class for my Contact Entity which happens to have an Email Property:

 

public class Contact

{

    [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.")]

    public string Email { get; set; }

}

 

Let's go ahead and add a DataTypeAttribute to the Email Property that better specifies what the Email Property Represents - a valid email address. Notice the new attribute, [DataType(DataType.EmailAddress], shown below on the Email Property:

 

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; }

}

 

So what does this give us? Well, the templated helpers look for a number of attributes in System.ComponentModel.DataAnnotations and one of them is the DataTypeAttribute. The DataTypeAttribute can affect a couple of things, but for this tutorial we will stick to one specific area - how the Email is displayed!

In my view I used the Html.DisplayFor Template Helper to display the email address of the Contact as such:

 

Html.DisplayFor(c => c.Email)

 

Without the DataTypeAttribute on the Email Property you would normally see this out of the box:

 

DataTypeAttribute

 

With the DataTypeAttribute that specifies the value specifically as an Email Address, you see the following out of the box when using the Html.DisplayFor Template Helper in ASP.NET MVC 2.0. Yeah, you got it right. That is a mailto: hyperlink automatically done for you when in display mode. Very cool!

 

DataTypeAttribute

 

This is the the tip of the iceberg and of course you can start doing some wonderful things just with the attributes in System.ComponentModel.DataAnnotations. We'll explore other techniques in future posts.

For those of you in the Tampa, Florida area, I will be showing off ASP.NET MVC 2.0 Preview 1 at the next Tampa ASP.NET MVC Developer Group Meeting. I will also be presenting this at the Southwest Florida Code Camp II.

Hope this helps,

David Hayden

 

ASP.NET MVC Book Reviews

 

posted on Tuesday, August 11, 2009 9:52 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