开发者

How to fail Ajax.BeginForm in ASP.Net MVC?

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 t开发者_如何学运维his 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?


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.


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"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜