首页 > 解决方案 > 在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"));
    }

类别.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;
          }
      })
  }
}

索引.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>

标签: jqueryajaxasp.net-mvc

解决方案


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

也许您可以尝试使用 GET ......


推荐阅读