UPDATE 6/Jul/2011: This post is now depreciated. Now that ASP.NET MVC 3 supports Unobtrusive AJAX & Validation with the jQuery Validation plugin with support for both EF 3.5/4.0+ and Linq To Sql OUT OF THE BOX! I wouldn’t bother with anything else! Great validation is now built-in. For a full tutorial of implementing client and server side validation using the jquery validate plugin shipped with ASP.NET MVC 3 click here, and here (under “JavaScript”) and tons more on Google here, courtesy LMGTFY!
I just wanted to do a “refresher” post regarding ASP.NET MVC validation as many things have changed over the last couple of months and there is a lot of information out there. I wanted to aggregate my known validation options and recommend them in one tabular list. Whilst I’m scared to write “best practice” now a days, I do believe that the below list is really worthwhile, and are the most prominent and reputable on the net, and one in particular, the xVal framework I am using in a real-world project with absolutely no hassles and no apparant drawbacks. However, I write that disclaimer as there is always something else out there
. So without further ado, Ill get to the list, which is hopefully simple and easy to follow.
I hope this list can help alleviate any confusion amongst anyone not sure on the best path to go down, or at least provide a good starting point for those of you new to ASP.NET MVC and a validation strategy.
Technique/tool Author Release date Server-side support
(validation performed by .NET out-of-the-box)Client-side support
(validation performed by JavaScript out-of-the-box)Client “ajax” server-side support
(validation performed by JavaScript AJAX call to server-side .NET validation out-of-the-box)ORM-targeted Rating xVal Framework (tutorial here / L2S? See below.) Steve Sanderson Jan 10 2009 *
devermind.com: Client-side form validation made easy (w/ xVal) Adrian Grigore Jun 21 2009 Validator Toolkit for ASP.NET MVC (haven’t tried it!) Jürgen Bäurle Jan 29 2008 ? ? ??? Error Handling with built-in ModelState object Microsoft Framework release Error handling with built-in IDataErrorInfo Interface Microsoft Framework release Error handling with built-in Data Annotation Model Binding Interface Microsoft Framework release
WHAT I RECOMMEND:
xVal + if you need client-side “ajax” validation calls, Adrian Grigores’ RemotePropertyValidator API which snaps right on to xVal.
* IF YOU CHOOSE XVAL (GREAT!):
Are you using LINQ to SQL and wish to declare your validation rules via .NET Data Annotations attributes? If so, click here.
(This outlines how to get this working so L2S re-generation doesn’t interfere with your property attributes for your model.)
If anyone has seen my older validation articles you’ll notice I had an Emad Ibrahim “validation hybrid”, but since xVal came along this is my framework of choice and also Emad’s didn’t support Linq to Sql.
If you have any others, comment and I’ll post them for others.
6 Comments
Super helpful! Thanks.
very good article
thank you very much
Check this out.
http://aspnetmvcval.codeplex.com/
Great guide, thanks a heap man.
I believe MVC2 and MVC3 both have built in validation using data annotations – and if I recall correctly MVC2 also starting with including JavaScript to do validation on the client side.
MVC3 now also has unobtrusive JavaScript validation, that adds ‘data-’ attributes to HTML elements, allowing JavaScript to be externalized.
Perhaps an update to this post is needed
I would be interested to hear if you’re still using xVal now that it’s ‘deprecated’. I actually never knew that xVal was created by Steve Sanderson – who now works for Microsoft – only knew of him through knockout.js.
Yes James that’s right, I’ve now updated the post
Hard to keep a lid on older posts sometimes, thanks for the heads up.
One Trackback/Pingback
[...] techniques here Possibly related posts: (automatically [...]
Post a Comment