I am using an MVC 3 Ajax.BeginForm call for form submission with client side validation. One of the input fields is a postcode (postalcode) and as well as validating the format I also want to check to see if it exists in a database table of approximatley 2 million entries.
The solution I chose was to use the an BeginForm OnBegin call to perform a lookup before submission and if the postcode does not exist offer the user the options of accepting it or re-entering. I am performing the postcode lookup using another Ajax call
The problem is that I need to wait for the the inner Ajax call to return and process the response before returning to the outer call but as this is an asynchronous call the function is continusing before the inner Ajax has completed.
I can see several non-preferred solutions, e.g. making the inner Ajax call synchronous or catching the submit button click event but does anyone have suggestions for a clean unobtrusive solution.
Thank you.
You could use the
[Remote]attribute to perform remote validation. You simply decorate the corresponding property on your view model with this attribute and then specify the controller action that will perform the actual validation.Obviously no matter which solution you choose you absolutely must perform the same check once the form is submitted on the server because by the time you initially checked with AJAX and the time the form is actually submitted your database data could change and what was valid initially be no longer valid.