ASP.NET MVC AJAX调用返回与Partial View并行的错误消息 [英] ASP.NET MVC AJAX call return Error Message in parallel with Partial View

查看:150
本文介绍了ASP.NET MVC AJAX调用返回与Partial View并行的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个场景是在一个ASP.NET MVC(4,5)中,最大化一个返回局部视图的ajax调用。有时候根据不同的情况,我可能需要返回错误信息 - 为用户显示某些东西,例如..

The scenario is in an ASP.NET MVC (4,5) maxing an ajax call that returns a partial view. Sometimes however based on different situations I may need to return an error message - display something for the user for example..

我目前的做法是这样的。

My current approach is this.

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

然而,我知道这是一个黑客而不是正确的解决方案有什么更好的办法吗?

I am however aware this is a hack and not a proper solution.. Is there any better way of doing this?

推荐答案

您可以在失败案例中返回一个 JsonResult 你可以添加你想要的Json的任何属性。

You could return a JsonResult in your fail cases. You could add any properties you want to your 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电话中,您可以执行以下操作:

In your ajax call you could do this:

$.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 you absolutely need to also return a failed status code, you could set it before returning the json such as:

if (id == 0)
{
    Response.StatusCode = 500;

    return Json(new
    {
        Failed = true,
        FailType = "Type1", // Whatever makes sense to you
        //OtherRelevantProperty = whatever
    });
}

然后,您可以处理您的失败回调:

And then you could handle the failed case in your fail callback:

$.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
        }
    }
});

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

Please note that I haven't tested this particular code and it is for demonstration only. I am, however, using a similar set-up in my projects.

这篇关于ASP.NET MVC AJAX调用返回与Partial View并行的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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