我正在使用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中定义的函数永远不会被调用,而我得到的是“无法找到资源”。但该项目已成功删除。
赞赏各种帮助。
答案 0 :(得分: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
}
});
}
});