【问题标题】:OnComplete Ajax.ActionLink parameter not Json in MVC 3?OnComplete Ajax.ActionLink 参数不是 MVC 3 中的 Json?
【发布时间】:2011-01-24 21:32:46
【问题描述】:

我正在使用 MVC 3。我在控制器上有一个返回 Json 对象的方法,根据这个问题,它应该作为 Json 返回给我,但我发现情况并非如此: ASP.NET MVC3 - Bug using Javascript

这是我的代码:

function DeleteItem(obj) {
alert(obj.responseText);
alert(obj.Success);
}
</script>
</head>

<body>
@Ajax.ActionLink("test", "Delete", "Home", new { id = "test" }, new AjaxOptions { Confirm = "Delete?", HttpMethod = "Post", OnComplete = "DeleteItem" });
</body>

还有控制器:

[HttpPost]
public ActionResult Delete(string id)
{
   return Json(new{Success = true,objectId = "testing"});
}

第一个消息框显示响应文本,即: {"Success":True, "objectId":"testing"}

第二个消息框显示未定义

所以它会正确返回给客户端,我只是不确定如何将其取出?

...斯蒂芬

【问题讨论】:

    标签: ajax asp.net-mvc-3


    【解决方案1】:

    Ajax.* 助手从来没有很好地工作过。尝试使用带有 jquery 的普通 Html 助手:

    @Html.ActionLink("test", "Delete", "Home", new { id = "test" }, new { id = "delete" })
    

    然后在一个单独的 javascript 文件中:

    $(function() {
        $('#delete').click(function() {
            if (confirm('Delete?')) {
                $.post(this.href, { }, function(result) {
                    alert(result.Success);
                });    
            }
            return false;
        });
    });
    

    【讨论】:

    • 感谢您的建议。我仍然很想知道如何让 Ajax.* 助手工作,但同时你的解决方案确实有效。
    • 可能我很幸运从未在 mvc 中使用过任何 Ajax.* 助手,但只是想知道为什么它们不能正常工作?
    【解决方案2】:

    您可以像这样重建对象。它对我有用。

    Json: {"message":"hello", "success": true}
    
    function getJsonDetails_OnComplete(res) {
    
        var obj = eval("(" + res.responseText + ")");
    
        alert(obj.message);
    };
    

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 2011-09-16
      • 2011-10-20
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多