为什么我在尝试删除项目时收到错误消息 404 not found

问题描述

我有一个问题,因为我是 ASP.NET MVC 的初学者;我尝试在按钮点击时使用 AJAX 调用创建一个 Delete 方法

当我按下删除按钮时,我收到错误“404 Not found”。我按照我找到的教程进行操作,但无法从索引页中删除项目。

我创建了这个控制器操作方法

[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 { @class = "btn btn-danger"  })
                         </td>*@
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

在我的脚本中我创建了这个函数

$(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","error");

当我检查控制台时,我看到此错误消息

加载资源失败:服务器响应状态为 404(未找到)患者/DeleteConfirmed:1

有谁知道我哪里出错了?这里有什么问题?

解决方法

你试过这些方法吗?

return RedirectToAction("Index","Folder_Name_Where_The_Index_Controller_Is");

只有当函数在调用它的同一个文件中时,这才有效。

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");
        }