1

I have Ajax.BeginForm with OnSuccess & OnFailure javascript handlers.

How do fail from my controller so that OnFailure is called?

Yes, I used to call throw new HttpException(404) and it used to work.

But now, I have set a custom error handler that calls Server.ClearError(). Due to this Ajax.BeginForm thinks that no error has occurred.

I am using the error handler given here: ASP.NET MVC Custom Error Handling Application_Error Global.asax?

Community
  • 1
  • 1
Gautam Jain
  • 6,789
  • 10
  • 48
  • 67

2 Answers2

1

You should just be able to throw an Exception. HttpException(404) is Not Found, which I don't think counts as an exception for OnFaiulre. Anything that results in a HTTP 500 should be interpreted as an error by the script.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • I think it is anything above the 2xx range, but regardless, if customerrors is on in web.config, it will never get called. – gangelo Feb 23 '12 at 03:48
0

The way I handle this, is to set the Response.StatusCode = 500 and append a Header to my response object Response.AppendHeader("MyResponseHeader", "My Message");

In my .js OnFailure handler...

function OnFailure(ajaxContext) {
    alert(ajaxContext.status); // 500
    alert(ajaxContext.getResponseHeader("MyResponseHeader"));  // "My Message"
}
gangelo
  • 3,034
  • 4
  • 29
  • 43