Limitation of ModelState.IsValid in ASP.NET MVC 3 - asp.net-mvc-3

I always use ModelState.IsValid for check all of my model validation validated correctly in Server Side, but I think there is a limitation to use this. For example I define a Remote Validation attribute, but if I disable javascript then ModelState.IsValid don't check Remote Validation and always return true, Where is the problem? this is a limitation for ModelState.IsValid or is my fault? If necessary I can Add all my implementation.

This question has come around a few times. The answer is: it doesn't validate on the server-side, you have to perform the validation action yourself. See also following SO posts:
asp.net mvc 3 serverside remote validation not working on submit through fiddler
RemoteAttribute validator does not fire server-side
Of course, it would be nice to be able to validate it anyway on the server-side. Luckily some nice guy made an implementation for it. You can find his short blog post: http://www.tugberkugurlu.com/archive/asp-net-mvc-server-side-remote-validation .

Related

File input does not post in ASP.NET Ajax.BeginForm. Why?

Let me first be clear about what I'm not asking. I can see that the native ASP.NET MVC Ajax.BeginForm does not handle posting files from within an AJAX form post. There are answers I've seen that seem to establish this fact, and to offer an assortment of workarounds via jQuery plugins, etc:
Ajax.BeginForm in MVC to upload files
File upload with ajax in asp.net mvc
I'm not asking whether it can't be done, and I'm not asking for a workaround. I'm hoping someone can explain why it can't be done.
How is the AJAX form post being handled in MVC such that file inputs do not post? I guess I had always assumed that it was being handled at a base level just like a full post, but with some javascript trimmings that prevented a full page reload. Apparently that's not the case. Is the form submitted via javascript something like jQuery's $('#form').submit()? What's happening under the covers?

Unobtrusive Javascript Validation with MVC 3, is preventing me validating form manually

For 90% of my site the standard MVC annotation with client script method is working a treat. But I have a form on the site that is quite complicated with multiple instances of dynamic form content dependant on answers to questions etc.
If I have the unobtrusive script included on the page, it's capturing the form submit and not allowing my custom jquery validate to validate the form.
I don't really want to refactor the site to have a seperate layout to remove the script when it's not needed. I wondered if there was an easy way to give control back to my custom validate script.
Any help would be great.
In your view you can disable client side validation like this
Html.ViewContext.ClientValidationEnabled = false

ASP.NET MVC 3 validation: Are the DataAnnotation attributes useless when JavaScript is disabled?

Scott Guthrie blogged about ASP.NET MVC 2: Model Validation little over a year ago and in his post, the controllers were decorated with calls to ModelState.IsValid-method. Since then we've had the ASP.NET MVC 3 which included quite big changes to the validation.
But has the requirement to call the ModelState.IsValid still stayed the same? Are all the DataAnnotation attributes useless if the site visitor has disabled the JavaScript and the site developer has forget to check the value of ModelState.IsValid?
If yes, is there a way around this? Is it for example possible to register a global filter which always remembers to check for the model's validity event if the coder doesn't?
The client side validation features would be turned off. This is why you must never rely only on client side validation. It would not affect the model binder which uses the annotations on the server. Here is the relevant text from that blog post...
Because the action method accepts a
“Person” object as a parameter,
ASP.NET MVC will create a Person
object and automatically map the
incoming form input values to it. As
part of this process, it will also
check to see whether the
DataAnnotation validation attributes
for the Person object are valid. If
everything is valid, then the
ModelState.IsValid check within our
code will return true – in which case
we will (eventually) save the Person
to a database and then redirect back
to the home-page.

Enforcing RemoteAttribute in MVC 3.0 when client side validation fails

I have an MVC object (3.0) that uses the RemoteAttribute to verify whether a name already exists in a database. This works fine, however in testing we are having instances where people clicking submit rapidly enough can get a form submission through before the validation finishes.
My first thought to this is to make sure that validation occurs on the server side, as well. Is there any way to enforce this without writing another custom validator attribute?
It is a good practice to enforce validation on the server side because relying only on client side validation is not enough. So writing another custom validation attribute is necessary.
Did you forget if( ModelState.IsValid ) ... on the server side ?

MVC2 Validation

My application uses MVC validation explained here:
http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style/
And it works fine with both server and client validation when the form is posted. However my problem is that I would like to have client validation on one of the fields before the form is posted. So when the TextBox loses focus it is validated directly.
I have solved it by using jQuery validation on the field that is validated before the form is posted.
You must use JQuery validations so that it will validate before posting. It also highlights those area where validation fails.

Resources