【问题标题】:Javascript and MVC return typesJavascript 和 MVC 返回类型
【发布时间】:2011-07-02 15:00:28
【问题描述】:

我在一个 ASP.Net MVC 项目的页面中有这个 JavaScript:

function showAllianceMembers_onclick() {
    var strName = $("#allianceNameTextBox").val();

    $.ajax(
    {
        type: "POST",
        url: "/Alliance/Index",
        data: "allianceName=" + strName,
        success: function (result) {
            if (result.success) {
                alert("done!");
            }
            else {
                alert("error!!");
            }
        },
        error: function (req, status, error) {
            alert(error);
        }
    });
}

如您所知,此脚本调用的是MVC Action。这是MVC Action

[HttpPost]
public ActionResult Index(string allianceName)
{
    //Populating an object containing a list (IList)

    return View(res);
}

这里的问题是 JavaScript 代码只显示带有错误消息的警报... 我的代码有什么问题?

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    在您的控制器操作中,您发送的不是 JSON,而是一个简单的视图。所以result 变量上没有定义.success 属性。您的 AJAX 请求如下所示:

    $.ajax({
        type: "POST",
        url: "/Alliance/Index",
        data: { allianceName: strName },
        success: function (result) {
            // If you got so far the AJAX request succeeded, the result variable
            // will contain the final HTML of the rendered view
            alert("done!");
        },
        error: function (req, status, error) {
            alert(error);
        }
    });
    

    或者如果您想从控制器操作发送 JSON:

    [HttpPost]
    public ActionResult Index(string allianceName)
    {
        // populate the result        
        return Json(new { success = true, result = res });
    }
    

    然后:

    $.ajax({
        type: "POST",
        url: "/Alliance/Index",
        data: { allianceName: strName },
        success: function (result) {
            if (result.success) {
                // do something with result.res
                alert("done!");
            }
        },
        error: function (req, status, error) {
            alert(error);
        }
    });
    

    【讨论】:

    • 我应该把ActionResult改成JSonResult吗?
    • @Dr TJ,不,你不应该。 Json 方法返回一个从 ActionResult 派生的 JsonResult。因此,为了更通用,请将 ActionResult 保留在您的操作签名中。
    【解决方案2】:

    我假设您正在尝试检索数据,而不是 HTML 标记。
    试试这个:

    $.ajax(
    {
        type: "POST",
        url: "/Alliance/Index",
        data: { 'allianceName' : strName },
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                alert("done!");
            }
            else {
                alert("error!!");
            }
        },
        error: function (req, status, error) {
            alert(error);
        }
    });
    

    并更新这个:

    [HttpPost]
    public JsonResult Index(string allianceName)
    {
        //Populating an object containing a list (IList)
    
        return new JsonResult { Data = res };
    }
    

    【讨论】:

    • 他不会从他的动作中返回 JSON,而是返回一个 HTML 视图。
    • 啊,您已经更新了您的答案以考虑到我的评论,删除了我的反对票。
    • @Darin,你不知道他究竟打算返回什么。我有一种感觉,这实际上是问题所在 - 他试图获取一些数据,但却返回了视图。
    • 是的,这就是我在回答这个问题时提出两种解决方案的原因。当您回答您的问题时,您只建议他使用 JSON,但您并没有指出操作代码也应该修改。
    • 您可能还想查看 jQuery 模板,以便对从 AJAX 调用返回到 HTML 表的数据进行客户端数据绑定:api.jquery.com/category/plugins/templates
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2014-01-13
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多