简体   繁体   中英

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.

PS 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM