你好我试图删除一行但是当有关系时它不会删除它但仍然返回true
public ActionResult Delete(int? User)
{
if (!User.HasValue)
return Json(new { IsComplete = false });
try
{
var item = Db.Institutions.FirstOrDefault(p => p.Id == User.Value);
Db.Institutions.Remove(item);
Db.SaveChanges();
}
catch
{
return Json(new { IsComplete = false });
}
return Json(new { IsComplete = true });
}
这是删除,如你所见,这是脚本;
$(document).on("click", ".m-portlet__nav-link", function () {
var myDataText = $(this).data('title');
var myDataId = $(this).data('id');
var myDataUrl = $(this).data('url') + "/" + myDataId;
$(".modal-body .dataIdFromJs").text(myDataText);
$("#saveChanges").click(function () {
var User = myDataId;
$.ajax({
dataType: "json",
type: "POST",
contentType: "application/json",
url: '@Url.Action("Delete", "Institution")',
data: JSON.stringify({ 'User': User }),
success: function (data) {
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": true,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
toastr.success("İşleminiz başarıyla tamamdlandı.");
},
error: function (data) {
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": true,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
toastr.error("Hata, tekrar deneyiniz!");
}
});
});
});
当没有关系时它正在工作并且返回true也删除了行但是当存在关系时它不会删除该行但仍然返回true如果由于关系而未删除行时如何返回false
答案 0 :(得分:0)
在服务器上发生意外(即404错误)之前,AJAX调用中的error: function (data)
永远不会被调用。
根据您编写的代码行总是落在success: function (data)
中,因为您正在处理异常并返回一些响应。
正如@Stephen Muecke在评论中所建议的那样,你不应该使用catch来调用AJAX的error
函数或者只是检查
success: function (data)
{
if(data.IsComplete)
{
// success toast
}
else
{
//error toast
};
}