【问题标题】:ASP.NET MVC AJAX call return Error Message in parallel with Partial ViewASP.NET MVC AJAX 调用与局部视图并行返回错误消息
【发布时间】:2017-03-08 17:31:14
【问题描述】:

该场景是在 ASP.NET MVC (4,5) 中最大化返回部分视图的 ajax 调用。但是,有时根据不同的情况,我可能需要返回错误消息 - 例如为用户显示一些内容..

我目前的做法是这样的。

在 JS 中:

$.ajax({
    url: url,
    type: "POST",
    data:{ id: id},
    success: function (response) {
        $("#container").html(response);
    },
    error: function (er) {

        if (er.status == "405")
            {// do someth }
        else if (er.status == "406")
                {// do someth else }
    }
});

在控制器中:

public ActionResult ServerMethod(int id)
{
    if (id = 0)
        return new HttpStatusCodeResult(405);
    if (id = 1)
        return new HttpStatusCodeResult(406);

    //otherwise.. 
    return PartialView("View", Model);
}   

但是我知道这是一个 hack 而不是一个正确的解决方案。有没有更好的方法来做到这一点?

【问题讨论】:

    标签: ajax asp.net-mvc error-handling http-error


    【解决方案1】:

    您可以在失败的情况下返回JsonResult。你可以将任何你想要的属性添加到你的 Json 中。

    类似这样的:

    public ActionResult ServerMethod(int id)
    {
        if (id == 0)
        {
            return Json(new
            {
                Failed = true,
                FailType = "Type1", // Whatever makes sense to you
                //OtherRelevantProperty = whatever
            });
        }
    
        if (id == 1)
        {
            return Json(new
            {
                Failed = true,
                FailType = "Type2", // Whatever makes sense to you
                //OtherRelevantProperty = whatever
            });
        }
    
        //otherwise.. 
        return PartialView("View", Model);
    }   
    

    在您的 ajax 调用中,您可以这样做:

    $.ajax({
        url: url,
        type: "POST",
        data:{ id: id},
        done: function (response) {
            if (response.Failed) {
                // Switch by response.FailType and use any relevant
                // json properties you created in the controller
            }
            else {
                $("#container").html(response);
            }
        }
    });
    

    如果你绝对需要返回一个失败的状态码,你可以在返回 json 之前设置它,例如:

    if (id == 0)
    {
        Response.StatusCode = 500;
    
        return Json(new
        {
            Failed = true,
            FailType = "Type1", // Whatever makes sense to you
            //OtherRelevantProperty = whatever
        });
    }
    

    然后您可以在 fail 回调中处理失败的情况:

    $.ajax({
        url: url,
        type: "POST",
        data:{ id: id},
        done: function (response) {
            $("#container").html(response);
        },
        fail: function(xhr) {
            var response = JSON.parse(xhr.responseText);
            if (response.Failed) {
                // Switch by response.FailType and use any relevant
                // json properties you created in the controller
            }
        }
    });
    

    请注意,我没有测试这个特定的代码,它仅用于演示。但是,我在我的项目中使用了类似的设置。

    【讨论】:

      【解决方案2】:

      你可以设置

      Response.StatusCode = 405;
      

      然后返回视图模型

      return PartialView("View", Model);
      

      然后在错误部分进行追踪?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 2016-01-07
        • 2012-01-04
        • 2013-05-11
        • 1970-01-01
        相关资源
        最近更新 更多