0

我正在尝试从表中删除项目。它有 Ajax 链接。

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

它使用 POST 方法从控制器调用 DeleteConfirm 方法。我做了一个简单的控制器,它应该做一些事情,所以 ActionLink 应该捕获错误并运行 OnFailure 函数(以显示警报对话框)。

控制器:

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

从控制器方法返回什么以便 OnFailure 函数调用?

4

3 回答 3

2

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;}
}

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.

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);
  }
}
于 2014-01-26T21:08:29.587 回答
1

how about throwing an exception?

public ActionResult DeleteConfirm(int id)
    {
        try
        {
            //make your call to the database here
            return View();
        }
        catch (ExceptionType1 ex)
        {
            //log the details of your error for support purposes, alerting tracing etc. 
            ex.Message = "Nice message for user"
            throw ex;
        }
        catch (ExceptionType2 ex)
        {
            //log the details of your error for support purposes, alerting tracing etc. 
            ex.Message = "Another nice message for user"
            throw ex;
        }
    }

Your ajax call would then know it was a failure, and run the correct function.

Edit I have added a second exception type to satisfy the comments. I still feel this is a good answer, and that the down vote is unfair.

于 2013-02-08T12:36:01.933 回答
1

OnFailure 将根据结果的状态代码触发,所以这样的事情会产生预期的效果。

return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Reason for failure");

另外,不确定它是否相关,但您的 OnFailure 文本不应该是“AlertDialog”而不是“AlerDialog”吗?

编辑:在您的控制器操作中,您应该能够使用 MVC 提供的扩展方法来测试请求是通过 Ajax 发出的Request.IsAjaxRequest()。请注意,没有真正的方法来检查请求是否是服务器上的 Ajax 请求,此方法利用了它所发出的所有 ajax 请求的自定义标头 jQuery 集的存在,换句话说,不要Request.IsAjaxRequest()在业务逻辑中使用。

IsAjaxRequest() 方法的来源

namespace System.Web.Mvc
{
    public static class AjaxRequestExtensions
    {
        public static bool IsAjaxRequest(this HttpRequestBase request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
        }
    }
}
于 2013-02-08T18:08:36.787 回答