2

Is it possible to include Ajax in a client-side validation script (using jQuery unobtrusive validation as the framework) and still have validation behave properly?

I have a validation scenario in which the client needs to send some user-entered data to the server, examine the returned data, and ask the user whether they want some of the entered data to be replaced with the fetched data. There's enough going on that I need custom javascript: I can't just use the Remote attribute, and there are no hooks in the Remote attribute for additional javascript processing.

This has to be run before submission, and has to be the last or one of the last things run before submission, so I've made it part of the validation. If the user answers "no" to the question about replacement, the submission doesn't happen, another way in which it resembles conventional validation.

My concern is that Ajax, by definition, is asynchronous, and validation scripts are usually if not always synchronous. So I'm facing having the validation routine need to return a value before the Ajax call has finished.

I know I can turn the async behavior off in jQuery for the length of the jQuery.getJSON call, but that's a bit inelegant. Is this what the MVC3 RemoteAttribute client-side does? Is there a better way?

0

Your Answer

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

Browse other questions tagged or ask your own question.