Vex
Vex

Reputation: 1239

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

Upvotes: 3

Views: 8236

Answers (4)

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

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.

Upvotes: 2

CrazyDart
CrazyDart

Reputation: 3801

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.

Upvotes: 4

Chris
Chris

Reputation: 3201

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

Upvotes: 2

Zruty
Zruty

Reputation: 8687

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

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

Upvotes: 2

Related Questions