jqueryasp.net-mvc-3remote-validation

ASP.Net MVC3: Remote validation calls not firing


I have a data entry form that has remote validation set up to check for existing values in the database for a certain field.

Here's what happens:

  1. Go to Create page (data entry form). Enter value X for field.

  2. Field loses focus. Remote validation is called. Fiddler shows the call.

  3. Click Save. Record is saved; page switches to Index page.

  4. Go to Create page again. Enter value Y again for field.

  5. Field loses focus. Remote validation is called (per Fiddler).

  6. Click Save. Record is saved; page switches to Index page.

  7. Go to Create page AGAIN and enter X (again) for field.

  8. Field loses focus. Nothing happens. No call to remote validation (per Fiddler).

  9. Click Save. No call to remote validation. Page attempts to save record but database spits it back.

It's as if the Create page is remembering that a previous value for the field was also X, and not revalidating it since it hasn't changed, even though I've been to other pages in the meantime.

I'm not passing a model to the Create view: I cannot imagine where it's getting the idea that that field has a previous value that's being (or not being) changed.

As far as I can tell, OutputCache is not being used. I'm on IE 8, if it matters.


Solution

  • OK, think I've figured it out. It's not just that you can't use OutputCache to cache your results, you have to explicitly say NOT to cache your validation results. See here for an explanation and example.

    ETA: Another thing that can happen is that if the first remote validation call fails (as in, returns a 500 error) it won't get called again. Because of this, if you anticipate your validation routine being called with null data from time to time (because your fields aren't filled in yet) you need to make the parameters nullable and account for that in your validation code.