OnComplete Ajax.ActionLink参数不是MVC 3中的Json?

时间:2011-01-24 21:32:47

标签: ajax asp.net-mvc-3

我正在使用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"});
}

第一个消息框显示响应文本,其中包括: {“成功”:是的,“objectId”:“测试”}

第二个消息框显示未定义

所以它正确地回到客户端,我只是不确定如何解决它?

...的Stefan

2 个答案:

答案 0 :(得分:1)

Ajax.*助手从来没有真正奏效过。尝试使用普通的Html帮助程序使用jquery:

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

答案 1 :(得分:0)

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

Json: {"message":"hello", "success": true}

function getJsonDetails_OnComplete(res) {

    var obj = eval("(" + res.responseText + ")");

    alert(obj.message);
};
相关问题