ASP.NET MVC3 and server side validation?

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.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

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... c# .net asp.net" rel="nofollow">asp.net asp.net" rel="nofollow">asp.net-mvc-3 link|improve this question edited Jul 26 '11 at 16:10 asked Jul 26 '11 at 15:49Vex978 80% accept rate.

I am having the same problem and don't know how to solve it. – Benjamin 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 Nov 18 '11 at 20:53.

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: bradwilson.typepad.com/blog/2010/10/mvc3... Just be sure to check model state once in the action for the server-side validation and everything will work great.

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 I finally got it to work. Thanks!

– Vex Jul 26 '11 at 16:24 Glad you got it fixed. – CrazyDart Jul 26 '11 at 19:12 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.

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.

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 I've added additional info.. – Vex Jul 26 '11 at 16:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions