ASP.NET 使用 cshtml 网页界面从数据库中删除行

ASP.NET delete row from database using a cshtml webpage interface

我在尝试按 ID 删除行时遇到问题。我在 table 中显示数据库的每一行,在每一行旁边都有一个按钮,用于删除数据库中的特定行。

它应该只删除具有该特定 ID 的行,但实际发生的是,我什至没有按下按钮,但在访问页面时 - 数据库的全部内容都被删除了。

我做错了什么?

到目前为止,这是我的代码:

控制器:

public class AdminController : BaseController
    {
        private UserContext context;

        public AdminController()
        {
            context = new UserContext();
        }

        public void delete_by_id(int id)
        {
            context.Users.Where(x => x.Id == id).DeleteFromQuery();
        }

        // GET: Admin
        [AdminMod]
        public ActionResult Index()
        {
            SessionStatus();
            if ((string)System.Web.HttpContext.Current.Session["LoginStatus"] != "login")
            {
                return RedirectToAction("Index", "Login");
            }
            var user = System.Web.HttpContext.Current.GetMySessionObject();
            UserData u = new UserData
            {
                Username = user.Username,
                Level = user.Level,
            };

            
            return View("Index", u);
        }
}

用户上下文:

public class UserContext : DbContext
    {
        public UserContext() :
            base("name=WebApplication1")
        {

        }

        public virtual DbSet<UDbTable> Users { get; set; }
    }

Index.cshtml:

@using WebApplication1.Controllers
@using WebApplication1.Domain.Enums
@using WebApplication1.Extension
@using WebMatrix.Data


@{
    ViewBag.Title = "Admin";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var db = Database.Open("WebApplication1");
    var selectQueryString = "SELECT * FROM UDbTables ORDER BY Id";

}

<script>
    function refreshPage() {
        window.location.reload();
    }
</script>

<div class="container" style="margin-top: 1%;">
    <h1>AdminPage</h1>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th scope="col">#Id</th>
            <th scope="col">Username</th>
            <th scope="col">Email</th>
            <th scope="col">Level</th>
            <th scope="col"></th>
        </tr>
        </thead>
        <tbody>
        @foreach (var row in db.Query(selectQueryString))
        {
            <tr>
                <th scope="row">#@row.Id</th>
                <td>@row.Username</td>
                <td>@row.Email</td>
                @{
                    if (@row.Level == 0)
                    {
                        <td>User</td>
                    }
                    else if (row.Level == 1)
                    {
                        <td>Premium</td>
                    }
                    else
                    {
                        <td>Admin</td>
                    }
                }
                <td>
                    <button type="button" data-bs-togle="modal" data-bs-target="#IdModal" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="Delete User" id="@row.Id" onclick ="refreshPage()">
                        @{
                            var smth = new AdminController();
                            smth.delete_by_id(row.Id); //HERE IS WHERE I CALL THE QUERY
                        }
                        <i class="fas fa-trash"></i>
                    </button>
                </td>
            </tr>

          }
        </tbody>
    </table>
</div>

这里我有证据表明为每一行分配了正确的 Id:

创建用于提交 POST 删除操作请求的表单。

<td>
    @using (Html.BeginForm("Delete", "Admin", new { id = row.Id }, FormMethod.Post))
    {
        <button type="submit" data-bs-togle="modal" data-bs-target="#IdModal" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="Delete User" id="@row.Id">
            <i class="fas fa-trash"></i>
        </button>
    }
</td>

AdminController中,需要一个Delete with [HttpPost]方法来执行删除操作,然后return查看。

public class AdminController : BaseController
{
    ...

    [HttpPost]
    public ActionResult Delete(int id)
    {
        delete_by_id(id);

        // Return desired view
        return Index();
    }
}