How to handle model state errors in ajax-invoked controller action that returns a PartialView

Posted by Robert Koritnik on Stack Overflow See other posts from Stack Overflow or by Robert Koritnik
Published on 2010-02-14T15:44:53Z Indexed on 2010/05/19 4:30 UTC
Read the original article Hit count: 286

Filed under:
|
|
|

I have a POST controller action that returns a partial view. Everything seems really easy. but. I load it using $.ajax(), setting type as html. But when my model validation fails I thought I should just throw an error with model state errors. But my reply always returns 500 Server error.

How can I report back model state errors without returning Json with whatever result. I would still like to return partial view that I can directly append to some HTML element.

Edit

I would also like to avoid returning error partial view. This would look like a success on the client. Having the client parse the result to see whether it's an actual success is prone to errors. Designers may change the partial view output and this alone would break the functionality. So I want to throw an exception, but with the correct error message returned to the ajax client.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery