views:

547

answers:

3

I haven't actually tried this yet and was hoping for a quick yes or no answer.

I am just learning about using JSonResult from a helpful stackoverflow question.

What I was wondering was can I use this kind of an actionresult, but still take advantage of the validation provided by the MVC model?

If I'm returning a Json result and theres a validation error will it just automatically return a different result type that is captured by the validation ajax logic?

+2  A: 

No, the built-in JsonResult will just serialize the object it is given to Json and send it down the wire. If you want to send validation errors, you would have to put that information in your own objects and send those through Json.

anurse
so all the validation model functionality only works with Form.Begin(...) right?
Simon_Weaver
Not exactly. You can display validation messages just fine without a form. The validation display information is stored in ViewData.ModelState. The HtmlHelpers you use for validation simply look at this object for rendering and don't require to be inside a form.
eyston
+2  A: 

What I've done is write my own ExtendedJsonResult class that inherits from JsonResult, originally to add the ability to register JavaScriptConverters to the serialization process, but this later allowed me to add a CheckContextForErrors method that looks at the modelstate errors of the context and adds and errors to the json result data (Property name, attempted value, message).

I implemented my own HandleJsonErrorInfo class (based off of HandleErrorInfo) and HandleJsonErrorAttribute that gets declared on Actions that will intercept any unhandled Exceptions and return the exception messages as JSON.

Client side I can check if an ExceptionMessages array exists in the JSON response and then iterate through each error.

Luke Smith
nice. [HandleError] was on my list of things to learn about (http://stackoverflow.com/questions/183316). Seems like a clever solution. I just wasnt clear if there was built in mechanism to return JSON errors. I've nvr used JSON before so i didnt even know if there was a convention for errors or not.
Simon_Weaver
+1  A: 

The default validation messages are created when you ModelBind (aka take request data and apply it to an object). This can be done via a standard form submit, or ajax, or any other means.

The validation messages are stored in ViewData.ModelState.

The rendering of the validation messages is done using HtmlHelpers that simply look at the ModelState and render any applicable messages. HtmlHelpers are done serverside as part of rendering the view. Returning JSON skips the rendering. You could instead return a partial view which would render any validation messages that are a part of the user control.

That said, If your controller action model binds and returns JSON, it will still generate the proper validation information in ViewData.ModelState. The information is there if you want to use it, but it needs to be moved to your Model and your client side javascript code needs to know what to do with it.

eyston
so theres nothing automatic. i'm fine doing it myself, just wanted to make sure there wasnt something clever provided for returning validation errors through JSON
Simon_Weaver