3

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

4
  • Could you please share your solution? I am having the same problem and don't know how to solve it. Commented Nov 17, 2011 at 23:16
  • I honestly don't remember the solution... sorry. Commented Nov 18, 2011 at 19:10
  • Actually, I've just remembered that if you hit F5 when the exception isthrown and just continue, the page should render properly. This type of exception will cause the debugger to break but it will not happen in production. Hope that helps. Commented Nov 18, 2011 at 19:16
  • You don't think people will enter incorrect data in a production environment? I beg to differ. Commented Nov 18, 2011 at 20:53

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's the problem, despite the use of data annotations and client validation working ok (when it's enabled), ModelState.IsValid returns true every time, even if the model is not in fact valid...
But HOW did you get it fixed? I'm having the same problem with EF at the moment.
2

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

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

Comments

2

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

Comments

2

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.

3 Comments

It's not quite working for me. If I disable client side validation, exceptions are thrown instead of error messages being displayed. I've tried using ModelState.IsValid but to no avail...
Then your question should have been "Why is my server side validation not working?" or something similar. Please post the exception that you are receiving and the code that is throwing it.
Please post the code for your model and controller. It will help us determine why the errors are occurring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.