views:

255

answers:

2

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?

+1  A: 

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.

Jace Rhea
A: 

As I'm looking for the same strategy I'm curious as to how you achieved displaying validation errors on the client? I'd like to achieve same visuals provided by <%= Html.ValidationMessage("model.XXX")%> with standard validation approach, i.e. returning ActionResult.

Sherlock