DefaultModelBinder Supports DataAnnotations and ValidationAttribute (s) in ASP.NET MVC 2.0

In ASP.NET MVC 1.0 you essentially had out-of-the-box support for IDataErrorInfo in the DefaultModelBinder. Although a step in the right direction, IDataErrorInfo requires a lot of manual effort to wire-up when validating custom types in your ASP.NET MVC Web Applications.

Most ASP.NET MVC Developers are overriding the default implementation of DefaultModelBinder and hooking in a custom validation framework, such as the Validation Application Block, NHibernate Validator, System.ComponentModel.DataAnnotations, or the myriad of other validation frameworks you can find for the .NET Framework.

In ASP.NET MVC 2.0 Preview 1, we now see built-in support for System.ComponentModel.DataAnnotations in the DefaultModelBinder as well as IDataErrorInfo Support. Looks to be a two-step process right now, but this could easily change. Essentially if you implement IDataErrorInfo and an error is detected, the DefaultModelBinder will not execute the additional support for System.ComponentModel.DataAnnotations validation. If an error is not detected using IDataErrorInfo ( or your classes don't support it ), then the DefaultModelBinder will look for ValidationAttribute (s) on your classes and implement the System.ComponentModel.DataAnnotations validation appropriately.

 

ValidationAttribute and System.ComponentModel.DataAnnotations

What this means to the ASP.NET MVC Developer is that they can now add ValidationAttributes to their classes for validating their custom types without needing to create a custom modelbinder.

 

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

}

 

The DefaultModelBinder in ASP.NET MVC 2.0 will automatically validate against those attributes and display the error messages appropriately in your ASP.NET MVC Web Applications.

 

ASP.NET MVC 2.0 DataAnnotations Support

 

Here is a snippet of the controller action:

 

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Edit(Contact contact)

{

    if (!ModelState.IsValid)

        return View(contact);

 

    // ...

}

 

Pretty cool.

Check out the Tampa ASP.NET MVC Developer Group as well as our next meeting where I will be presenting on ASP.NET MVC 2.0 Preview 1. I will also be presenting ASP.NET MVC 2.0 at the SouthWest Florida Developer Code Camp II.

 

David Hayden

 

ASP.NET MVC Tutorials

 

posted on Sunday, August 09, 2009 12:35 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