Good day!
I use ASP.NET MVC 2.0 with server validation (DataAnnotations + IDataErrorInfo for ViewModel classes).
I can write my client validation by hand using jQuery validation plugin (I've done this for years :)) and this is the only approach when I can get very smart client validation.
But for most cases it will be cool to pull server rules directly to client validation. I've looked here: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-vali开发者_JAVA百科dation.aspx but it seems to much code for such common task
Questions (at last):
- What is better solution?
- Will ASP.NET MVC 3 bring anything in this area?
Thanks!
There's no magic here. If you are performing some custom validation on your server you need to translate it to the client validation framework you are using. What if you are querying some distant server to perform business validation. How on earth do you expect the client to know about this? And no, ASP.NET MVC 3 doesn't bring anything new in this area.
What ASP.NET MVC 3 brings is unobtrusive validation for simple rules and making jquery validate the default client side validation plugin. Here's another nice article about it.
By the way I would recommend you looking at FluentValidation. It has a great integration with ASP.NET MVC.
精彩评论