I have an Ajax form that sends input data to a controller. I currently have an action result that returns the view and the ajax form helper has an ‘OnSuccess’ function it calls.
What I would like to do is that if the validation done in the controller fails, then have the POST return as a failure so I can use the OnFailure attribute in the AJAX helper to kickoff another function. Is this possible?
Also, I would prefer not to use Model Binding/validation that way since the Models I use are complex and do not lend themselves easily to binding.
As far as I recall (only just returning to the Ajax helpers), OnFailure is called on an HTTP Status Error, so if you make your view trigger one of those, the OnFailure function should be called, maybe something like the following in the action (going against the idea of exceptions being exceptional, and using an unorthodox status code (Conflict), but you get the idea):
Or, using the built in HttpStatusCodeResult:
Or, if you need to also return some actual content:
Finally, if you use option 3 a lot, you could create a new ActionResult (or ViewResult) subclass to handle setting the status code and returning the result.