[英]Why I get error message 404 not found when I try to delete Item
我有一個問題,因為我是 ASP.NET MVC 的初學者; 我嘗試使用 AJAX 調用按鈕單擊來創建Delete
方法。
當我按下刪除按鈕時,我收到錯誤“404 Not found”。 我按照我找到的教程進行操作,但我無法從索引頁面中刪除項目。
我創建了這個 controller 操作方法:
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Patient patient = db.Patients.Find(id);
db.Patients.Remove(patient);
db.SaveChanges();
TempData["success"] = true;
TempData["result"] = "Patient Has Been Successfully deleted";
return RedirectToAction("Index");
}
在我的Index
頁面中,我添加了操作名稱
<div class="panel panel-flat">
<div class="panel-body">
<table class="table datatable-responsive datatable-patients">
<thead>
<tr class="bg-blue">
<th>
Firstname
</th>
<th>
Lastname
</th>
<th>
Contact
</th>
<th>
Email
</th>
<th>
Blood Group
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr id="tr-id-@item.patient_id">
<td>
@Html.DisplayFor(modelItem => item.patient_first_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.patient_last_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.patient_contact_number)
</td>
<td>
@Html.DisplayFor(modelItem => item.patient_email)
</td>
<td>
@Html.DisplayFor(modelItem => item.patient_blood_group)
</td>
<td>
<ul class="icons-list text-right">
<li class="text-primary-600"><a href="~/Patients/Edit?Id=@item.patient_id"><i class="icon-pencil7"></i></a></li>
<li class="text-danger-600"><a class="delete_pacient" href="javascript:;" data-id="@item.patient_id"><i class="icon-trash"></i></a></li>
</ul>
</td>
@*<td>
@Html.ActionLink("Edit", "Edit", new { id = item.patient_id }, new { @class = "btn btn-info" })
@Html.ActionLink("Delete", "Delete", new { id = item.patient_id }, new { @class = "btn btn-danger" })
</td>*@
</tr>
}
</tbody>
</table>
</div>
</div>
在我的腳本中,我創建了這個 function
$(document).on('click', '.delete_pacient', 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: "/Patients/DeleteConfirmed",
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");
}
});
})
});
每當我嘗試刪除它只顯示消息
swal("Oops", "Something went wrong!", "error");
當我簽入控制台時,我看到此錯誤消息
加載資源失敗:服務器響應狀態為 404(未找到)患者/DeleteConfirmed:1
有誰知道我在哪里犯了錯誤? 這里有什么問題?
這些方法你試過了嗎?
return RedirectToAction("Index", "Folder_Name_Where_The_Index_Controller_Is");
僅當 function 位於調用此文件的同一文件中時,此方法才有效。
return RedirectToAction(nameof(Index));
你也可以使用這個
return Redirect("http://YourPage.com/Index);
如果這些沒有幫助,請將代碼替換為:
public async ActionResult DeleteConfirmed(int id)
{
Patient patient = db.Patients.Find(id);
db.Patients.Remove(patient);
await db.SaveChangesAsync();
TempData["success"] = true;
TempData["result"] = "Patient Has Been Successfully deleted";
return RedirectToAction("Index");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.