【问题标题】:Delete database records using jquery ajax in asp.net mvc在asp.net mvc中使用jquery ajax删除数据库记录
【发布时间】: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


【解决方案1】:

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

也许你可以用 GET 试试...

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2016-12-10
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2010-11-18
    • 2013-10-13
    • 2013-07-13
    相关资源
    最近更新 更多