0

我正在使用 MVC 3 开发一个 Web 应用程序,并希望在用户成功删除项目后向用户返回一条消息。

MyWallController 方法如下所示:

[HttpPost]
public ActionResult DeleteAlbum(Guid albumId)
{
    try
    {
        this.albumService.DeleteAlbum(albumId);
        return Json(new { success = true, msg = "Album successfully deleted" }, JsonRequestBehavior.AllowGet);
    }
    catch (FPSException e)
    {
        return Json(new { success = false, msg = e.Message });
    }
    catch (Exception)
    {
        throw new HttpException(500, "Error while deleting album");
    }
}

链接:

<a class="open-DeleteAlbumDialog" href="http://localhost:2941/MyWall/DeleteAlbum?albumId=0f49b1ad-8ec1-4fca-b8e2-28bdbf47824e">Delete</a>

JavaScript:

$(function () {
    $(document).on('click', '.open-DeleteAlbumDialog', function () {
    var answer = confirm('Are you sure you want to delete this album?')
    if (answer) {
        $.post(this.href, function (data) {
            if (data.success) {
                // do something
            } else {
                // do something else
            }
        });
    }
    else return false;
});

但是,在 post 中定义的函数永远不会被调用,我得到的是“找不到资源”。但该项目已成功删除。

各种帮助表示赞赏。

4

1 回答 1

2

您的链接仍然有效。你需要preventDefault

$(function () {
    $(document).on('click', '.open-DeleteAlbumDialog', function (e) {
    e.preventDefault();
    var answer = confirm('Are you sure you want to delete this album?')
    if (answer) {
        $.post(this.href, function (data) {
            if (data.success) {
                // do something
            } else {
                // do something else
            }
        });
    }
});
于 2012-10-28T21:12:03.187 回答