开发者

ASP.NET MVC3 and server side validation

Suppose a user has javascript disabled and thus client side validation doesn't work in MVC3.

What is the best way to implement server side validation so that validation messages are still displayed when the user tries to handle data in an inappropriate way?

Thanks!

EDIT:

Apparently it's happening because I'm using EF generated models and they use "StructuralObject.SetValidValue" methods in property setters. This results in an exception being 开发者_运维知识库thrown before MVC can validate the model.

I'm trying to find a way to circumvent this right now...


Well, you should always use client side and server side validation. If you mark the models with validation attributes both the server-side and client-side validation should work just fine.

I am sure you have seen this: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

Just be sure to check model state once in the action for the server-side validation and everything will work great.


ScottGu suggests in his blog to use Data Annotations for this.

P.S. The link is about MVC2, this one seems more recent.


Take a look at Scott Gu's blog on the topic. He does a walkthrough of how to handle this

ASP.NET MVC 2: Model Validation


If you are using Data Annotations for validation, you shouldn't need to do anything. The Server will always validate the data, regardless of whether the client has already done so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜