【发布时间】:2021-04-07 07:58:19
【问题描述】:
我有一个问题,很遗憾我看不到错误。问题如下。我创建 Delete 方法并在单击按钮时在 AJAX 中调用该方法。
但是当我转到索引页面并尝试删除项目时,我只收到我的 swall aller 显示的错误消息。
error: function (data) {
swal("Oops", "Something went wrong!", "error");
}
到目前为止,我在控制器中创建了Delete Action
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Language language = db.Languages.Find(id);
db.Languages.Remove(language);
db.SaveChanges();
return RedirectToAction(nameof(Index));
}
在我的Index Page 中,我创建了这个:
<div class="panel panel-flat">
<div class="panel-body">
<table class="table datatable-responsive datatable-languages">
<thead>
<tr class="bg-blue">
<th>
Language Name
</th>
<th>
Country Code (Flag)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr id="tr-id-@item.language_id">
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.code)
</td>
<td>
<ul class="icons-list text-left">
<li class="text-primary-600"><a href="~/Languages/Edit?Id=@item.language_id"><i class="icon-pencil7"></i></a></li>
<li class="text-danger-600"><a class="delete_languages" href="javascript:;" data-id="@item.language_id"><i class="icon-trash"></i></a></li>
</ul>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
还有我的 AJAX 调用:
<script>
$('.datatable-languages').DataTable({
dom: 'Bfrtip',
columnDefs: [
{
responsivePriority: 1,
targets: -1
},
{
targets: [-1],
orderable: false,
searchable: false,
printable: false,
width: "120"
}
]
});
$(document).on('click', '.delete_languages', function () {
var myId = $(this).attr('data-id');
console.log(myId);
var parent = $(this).parent().parent().parent().parent();
swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#FF7043",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function () {
$.ajax({
type: "post",
url: "/Languages/Delete",
data: { id: myId },
success: function () {
parent.fadeOut('slow', function () { $(this).remove(); $('.child').remove() });
setTimeout(function () { swal("Deleted!", "Record deleted successfully!", "success"); }, 2000);
},
error: function (data) {
swal("Oops", "Something went wrong!", "error");
}
});
})
});
</script>
之前的一个符号,我尝试删除 AJAX 调用,并在我的索引页面中添加了这行代码,它可以工作
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.BlogId }) <br />
@Html.ActionLink("Details", "Details", new { id = item.BlogId }) <br />
@Html.ActionLink("Delete", "Delete", new { id = item.BlogId })
</td>
谁能帮助我,告诉我哪里出错了,因为我找不到,也不知道从哪里开始找。一切看起来都不错,但无法删除项目,在控制台中我收到错误消息
内部服务器错误 (500)
【问题讨论】:
-
请尝试将您的网址替换为:
url: '@Url.Action("Delete")', dataType: 'json', -
在我的脚本中?在 AJAX 调用中?
-
你可以在 ActionResult 中添加 try catch 然后你必须调试,在 catch 上检查你的消息。如果不能调试,请使用你的 ajax
-
可能
DeleteConfirmed正在引发异常。由于您在那里没有任何异常处理,因此框架的后备是返回一个通用的 500 错误...尝试在您的删除处理程序中添加一些异常处理
标签: c# sql-server ajax asp.net-mvc datatable