ASP.NET MVC AJAX onError的处理 [英] ASP.NET MVC AJAX onError handling

查看:204
本文介绍了ASP.NET MVC AJAX onError的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从表中删除的项目。还有阿贾克斯链接吧。

I am trying to delete item from table. There is Ajax link for it.

@Ajax.ActionLink("Delete", "DeleteConfirm", new { id = Model.ID }, new AjaxOptions {
    HttpMethod = "POST", UpdateTargetId = "TableID", OnSuccess = "CloseDialog", OnFailure = "AlerDialog"
  })

它要求从POST方法控制器DeleteConfirm方法。我做了简单的控制器,它应该做的事这样的ActionLink应该捕获错误并运行onFailure处功能(以示警告对话框)。

It calls DeleteConfirm method from controller with POST method. I made simple controller which should do something so ActionLink should catch error and run OnFailure function (to show alert dialog).

控制器:

public ActionResult DeleteConfirm(int id)
        {            
            // code here
        }

怎么从控制器方法返回所以onFailure处功能调用?

What to return from controller method so OnFailure function invokes?

推荐答案

在服务器端上发生错误的OnError被激发。由于错误,我的意思是例外,我认为你不能在客户方传递异常消息,除了500服务器错误。
我认为这很好的形式给出就是有一些CustomResponse类,你的行动会回来。
对你来说,是这样的:

OnError is fired when error happens on serverside. By error, I mean exception, and I think you can't pass exception message on clientside except of 500 Server error. I think that good aproach is to have some CustomResponse class that your action will return. In your case, something like:

Class DeletionResponse
{
    public bool IsDeletionSuccesfull {get; set; }
    public string Message {get; set;}
}

在DeleteConfirm行动创建新的响应,这可能需要inheriteActionResult类(我不知道,因为我是新来的MVC)。如果某些错误ocures而删除,DeletionSuccesfull设置为false,并且信息给异常信息,或者一些自定义消息。

In DeleteConfirm action you create new response, which maybe needs to inheriteActionResult class(I'm not sure because I'm new to MVC). If some error ocures while deleting, set DeletionSuccesfull to false, and Message to message of exception, or some custom message.

在客户端,重点是研究在onSuccess处理成功,然后再决定该怎么做。
是这样的:

On client side, the point is to examine success in OnSuccess handler, and then decide what to do. Something like:

function handleResponse(deletionResponse){
  if(deletionResponse.d.IsDeletionSuccesfull){
     CloseDialog();
  }
  else{
     AlertDialog(deletionResponse.d.Message);
  }
}

这篇关于ASP.NET MVC AJAX onError的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆