asp.net mvc - Can i return JsonResult from an MVC action and still take advantage of validation? -


I have not tried it yet and he is not expecting instant yes or no answer.

I am currently learning about using JSONResult.

Am I wondering if I could use such an artificial way, but still I can take advantage of it?

If I returning to a JS result and a verification error occurs, will it automatically return a different result type, which is taken by validation, ajax logic?

When you receive ModelBind (aka request data and apply to an object) the default verification message becomes Go. This can be done through a standard form submission, or AJAX, or any other medium.

Verification message views are stored in DataModelState.

The verification message is presented using HtmlHelpers which only looks at the modelstate and presents an applicable message. As part of rendering the scene HtmlHelpers is left side of the server leaving the returning JSON rendering. You can return a partial view in turn, which is a part of user control that will present a verification message.

He said, if your controller binds action models and returns Jasonon, then it will still generate appropriate verification information in ViewData.ModelState if you want to use it, the information is here, but It should be moved to your model and your client-side JavaScript code needs to know what to do with it.


Comments