I've received a lot of email about the best way to handle validation in an ASP.NET MVC application. We posted three new tutorials on validation at the http://www.ASP.net/mvc website. Here is the list:

 

  • Performing Simple Validation - Learn how to perform validation in an ASP.NET MVC application. In this tutorial, Stephen Walther introduces you to model state and the validation HTML helpers.

 

  • Validating with the IDataErrorInfo Interface - Stephen Walther shows you how to display custom validation error messages by implementing the IDataErrorInfo interface in a model class.

 

  • Validating with a Service Layer - Learn how to move your validation logic out of your controller actions and into a separate service layer. In this tutorial, Stephen Walther explains how you can maintain a sharp separation of concerns by isolating your service layer from your controller layer.

 

I don't want to suggest that these are the only methods of performing validation in an ASP.NET MVC application. For example, an alternative that is not discussed in these tutorials is the Microsoft Enterprise Library Validation Application Block. In future tutorials, I'll discuss other methods of performing validation.

Here's the direct link to the MVC Validation tutorial category:

ASP.NET MVC Validation

If you liked this blog post then please Subscribe to this blog.
posted on Wednesday, March 04, 2009 5:29 PM | Filed Under [ ASP.NET ASP.NET MVC ]

Comments

Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by Bill
on 3/4/2009 7:29 PM

Thanks for posting these. One little problem... the formatting in the second tutorial (Validating with the IDataErrorInfo Interface) is borked. It appears there's a missing quote on the pre tags for code samples. Makes it unreadable.
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by Will Green
on 3/4/2009 7:44 PM

I second @Bill's comment. Article is totally unreadable due to an unclosed attribute value (missing close quotation mark) for the first code sample. Renders everything beyond it unreadable. Disappointing, as this is the very topic I'm struggling with on my app right now.
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by Stephen Walther
on 3/5/2009 6:29 AM

@Will, @Bill -- Thanks for pointing out the formatting issue -- it's been fixed.
Gravatar
# # re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
on 3/6/2009 12:53 AM

@bill and @Will - it look ok for me. I dont seem to be having problem to view the article.
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by Phillip
on 3/27/2009 7:16 AM

In the simple demo you mention needing to add resource strings to customize the prebinding error messages. Can you show an example of how to do this?
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by Rock
on 4/3/2009 4:21 PM

Is there any easy way to do validation for properties such as associations in Entity framework? Since there is no OnChanging for such properties... to use IDataErrorInfo?
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by tom
on 6/3/2009 9:08 AM

You may also use the Validator Toolkit for ASP.NET MVC on codeplex.com: <a rel="nofollow external" href="http://mvcvalidatortoolkit.codeplex.com" title="http://mvcvalidatortoolkit.codeplex.com">http://mvcvalidatortoolkit.codeplex.com</a>
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by internet faxes
on 6/15/2009 2:26 AM

Graet tutorials and links. Thanks for sharing them.
Gravatar
# re: New ASP.NET MVC Validation Tutorials Posted at www.ASP.net/MVC
Posted by MvcCms-Jon
on 6/16/2009 12:06 PM

Thanks for all the great input Stephen. I have tried to use a lot of the architecture you have outlined on MvcCms. We are nearing out first release and would appreciate any feedback on how we might improve the initial architecture. The source is available on codeplex at mvccms.codeplex.com
Comments have been closed on this topic.