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

Vex
  • 1,179
  • 3
  • 15
  • 24
  • Could you please share your solution? I am having the same problem and don't know how to solve it. – Benjamin Gale Nov 17 '11 at 23:16
  • I honestly don't remember the solution... sorry. – Vex Nov 18 '11 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. – Vex Nov 18 '11 at 19:16
  • You don't think people will enter incorrect data in a production environment? I beg to differ. – Benjamin Gale Nov 18 '11 at 20:53

4 Answers4

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.

CrazyDart
  • 3,803
  • 2
  • 23
  • 29
  • 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... – Vex Jul 26 '11 at 16:03
  • But HOW did you get it fixed? I'm having the same problem with EF at the moment. – Mohag519 Jul 27 '11 at 12:53
2

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

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

Zruty
  • 8,377
  • 1
  • 25
  • 31
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

Chris
  • 3,191
  • 4
  • 22
  • 37
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.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • 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... – Vex Jul 26 '11 at 16:00
  • 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. – Kyle Trauberman Jul 26 '11 at 16:05
  • Please post the code for your model and controller. It will help us determine why the errors are occurring. – Dismissile Jul 26 '11 at 16:08