Can I return JsonResult from an MVC action and still take advantage of validation?

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.

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.

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 Jan 27 '09 at 21:24.

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.

Nice. HandleError was on my list of things to learn about (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 didn't even know if there was a convention for errors or not.

– Simon_Weaver Jan 28 '09 at 2:01.

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.

So all the validation model functionality only works with Form. Begin(...) right? – Simon_Weaver Jan 27 '09 at 8:01 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 Jan 27 '09 at 13:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions