【发布时间】:2020-04-19 09:49:24
【问题描述】:
我正在尝试使用 jquery ajax 删除数据库记录。当单击删除按钮弹出消息出现并确认时我收到错误消息找不到资源和请求的 url:/Category/Delete/8。我写了一个单独的js文件名category.js
CategoryController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id)
{
_categoryService.Delete(id);
//return RedirectToAction("Index");
return Json(Url.Action("Index"));
}
Category.js
function Delete(id) {
var ans = confirm("Are you sure you want to delete this Record?");
debugger
if (ans) {
$.ajax({
type: "POST",
url: "/Category/Delete" + id,
success: function (response) {
alert("Successfully Deleted");
window.location.href = response;
}
})
}
}
Index.cshtml
<td>
@Html.ActionLink("Edit", "Update", new { id = category.CategoryID }, new { @class = "btn btn-warning" })
@Html.ActionLink("Delete", "Delete", new { id = category.CategoryID }, new { @class = "btn btn-danger", @onclick = "Delete('" + category.CategoryID + "');" })
</td>
【问题讨论】:
-
URL 应该有 id 作为它的一部分,并用 / 分隔它? url: "/Category/Delete/" + id
-
1) 您在
url:行上缺少斜杠。应该是url: "/Category/Delete/" + id。 2)您没有发送防伪令牌,因此您可能不想从控制器操作方法调用[ValidateAntiForgeryToken]。
标签: jquery ajax asp.net-mvc