asp.net-mvcvalidationxvalcastle-validators

What techniques are being used to pass MVC ModelState validation errors back to the client when using JsonResult?


I'm sort of thinking out loud here, so let me know if I need to clarify...

on ajax heavy sites, when using JsonResult to pass information back to the client, what techniques, patterns, best practices are being used to pass ModelState validation errors back to the client?

I am using xVal and castle validation on my view models, is there some sort of standard to get jquery validate to display errors coming from ajax responses?


Solution

  • I know of no best practices, but I can tell you what I did on a recent project. Basically, I defined an interface for all JSON save results called IJSONValidationResult. This interface consisted of two properties, a bool IsValid indicating if the Save/Action was valid and a List of the errors. The class was than populated with the ModelState validation errors.

    Than I used javascript on the client side to parse the JSON result and make the appropriate updates to the page. As an example, if it was valid than redirect from the edit page to the list page showing the saved result, or if there were errors, show them to the user in a previously hidden div.

    Nothing too exciting, but it was low overhead and pretty straight forward.