3

The Ajax request is as follows:

In the controller action method I tried to add ModelState Error as well as throw an exception but response is never calling OnFailure. It always calls the OnSuccess regardless of response. May be I missing somthing here.

The question: When is it going to call the OnFailure js method? Any samples will be greatly appreciated.

Thanks

Bharat
  • 3,491
  • 1
  • 22
  • 22

2 Answers2

12

You need to return an HTTP error status code from your action. E.g. 400, 404, 500.

try something like this:

throw new HttpException(404, "Not Found");
dso
  • 9,463
  • 10
  • 53
  • 59
  • The answer listed above should work. You can check my question [here](http://stackoverflow.com/a/37332246/4896260) which gives a bit more detail. – usr4896260 Jun 10 '16 at 14:15
0

As per the MSDN definition, OnFailure is called when the HTTP response status is not in the 200 range. For instance if its a permanent Redirect (308), temporary redirect (307) Not Found (404) or Bad Request (400) then the OnFailure method is called. For a full list of HTTP response codes, see this wikipedia article

Dev
  • 1,146
  • 2
  • 19
  • 30