How to handle model state errors in ajax-invoked controller action that returns a PartialView
- by Robert Koritnik
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.