Praveen Prasannan
Praveen Prasannan

Reputation: 7133

Give error to ajax call from controller's catch

I have a jquery ajax call to my controller action. Now in my controller, it went catch. Now I need to invoke error function of my ajax call. If I throw error, this will work. Is there any other method to invoke error function.

Bye the way, my controller's action's return type is string/void.

Upvotes: 1

Views: 2908

Answers (2)

Brian Ball
Brian Ball

Reputation: 12616

I've verified this now, so I'm posting it as an answer. You can throw an exception as @Timur mentioned, but that has overhead associated with it that you may not want/need.

In your action method, you can set Response.StatusCode to an error value (anything in the 300, 400, or 500 range), and it will cause jQuery to invoke the error handler instead of the success handler.

Upvotes: 4

Timur  Shahbanov
Timur Shahbanov

Reputation: 425

Write this in your Action Throw new HttpException(); This will invoke error to your ajax function and handle it with this $.ajax method function error:function() {do something }

Upvotes: 4

Related Questions