jquery - 在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>
解决方案
因为 Id 是通过 URL 发送的参数,所以我认为它应该是 HTTP GET 请求?
也许您可以尝试使用 GET ......
推荐阅读
- r - 按位置比较字符向量的相似度
- android - 单击 Paging 3 库中的项目加载更多数据
- slack-api - Slack API,如何获取机器人的用户范围?
- python - 人们如何在使用离散傅立叶变换进行数值积分时处理调整大小的网格步骤?
- java - Spring 无法连接通用 bean - 没有符合条件的 bean 类型
- cassandra - 当 index_summary_resize_interval_in_minutes 设置为 60 分钟时,Cassandra 开始超时
- api - 如何通过 CURL 通过 eks 集群解决 API 调用期间的 403 错误
- java - 如何分步读取分组等数据?
- netsuite - 单击按钮时在 Netsuite CS 脚本中调用自定义函数的问题
- ios - 我可以在我的物理 iPhone 设备上从 VSCode (Windows) 测试我的 Flutter 应用程序吗?