在asp.net mvc中使用jquery ajax删除数据库记录



我正在尝试使用jquery ajax删除数据库记录。当点击删除按钮时,弹出消息出现,当确认时,我收到错误消息资源找不到,请求的url:/Category/delete/8。我写了一个单独的js文件名category.js

类别控制器.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>

因为Id是通过URL发送的参数,所以我认为它应该是HTTP GET请求?

也许你可以试试GET。。。

最新更新