删除 post 方法 returns null

Delete post method returns null

我正在学习如何使用 asp.net mvc 制作简单商店的初学者教程,并且在教程中执行完全相同的步骤时没有问题。 我目前正在尝试在我的类别页面上执行基本的 CRUD 操作,但是在尝试删除类别时我被卡住了。我没有找到页面,因为 id 为 null,但是在传递相同的 id 参数时我对 Edit 方法没有问题。 我正在寻找答案,有些人建议可能存在缓存问题,但不确定如何尝试解决该问题。 这是我的删除操作控制器

 // GET-DELETE
        public IActionResult Delete(int? id)
        {
            
            if (id == null || id == 0)
            {
                return NotFound();
            }

            Category obj = _db.Category.Find(id);

         if (obj == null)
            {
                return NotFound();
            }

            return View(obj);
        }

        //POST-DELETE
        [HttpPost]
        [ValidateAntiForgeryToken]

        public IActionResult DeletePost(int? id)
        {
            Category obj = _db.Category.Find(id);

            if (id == null)
            {
                return NotFound();
            }
            
            
            _db.Category.Remove(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
           
           
        }

这是我的观点

@model RockyWebsite.Models.Category

<form method="post" asp-action="DeletePost">
    @Html.HiddenFor(id => id.CategoryId)
    <input asp-for="CategoryId" hidden />

    <div class="border p-3">

        <div class="form-group row">
            <h2 class="text-info pl-3">Delete Category</h2>
        </div>
        <div class="row">
            <div class="col-8">
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="CategoryName"></label>
                    </div>
                    <div class="col-8">
                        <input asp-for="CategoryName" disabled class="form-control" />

                    </div>

                </div>
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="DisplayOrder"></label>
                    </div>
                    <div class="col-8">
                        <input asp-for="DisplayOrder" disabled class="form-control" />

                    </div>

                </div>
                <div class="form-group row">
                    <div class="col-8 offset-4 row">

                        <div class="col">
                            <input type="submit" class="btn btn-danger w-100" value="Delete" />
                        </div>
                        <div class="col">
                            <a asp-action="Index" class="btn btn-success w-100"><i class="fas fa-sign-out-alt"></i> Back</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-4">
                @* Keep this empty *@
            </div>
        </div>
    </div>

</form>

如有任何帮助或建议,我们将不胜感激,谢谢!

在输入中添加名称标签,例如:

<input name="id" asp-for="CategoryId" hidden />

亚历克斯

您正在使用 @Html.HiddenFor(id => id.CategoryId)(好吧,您实际上也在使用 tag-helper 语法 <input asp-for="CategoryId" hidden />,您应该只使用其中之一,而不是同时使用!)将使用 name="CategoryId".

创建输入的视图

因此,最简单的解决方案可能是更正视图并更新 DeletePost 的控制器操作中的参数名称。

查看:

<!-- remove this line: @Html.HiddenFor(id => id.CategoryId) -->
<!-- just use line below -->
<input type="hidden" asp-for="CategoryId" />

控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int? categoryId)
{
    Category obj = _db.Category.Find(categoryId);
    // check obj here, not id
    if (obj == null)
    {
        return NotFound();
    }    
    
    _db.Category.Remove(obj);
    _db.SaveChanges();
    return RedirectToAction("Index");
}

用这个替换你的表单标签

@Html.BeginForm("DeletePost", "controllerName", FormMethod.Post,
 new {id="@Model.CategoryId"})
{
@Html.AntiForgeryToken()

和行动

[HttpPost("{id}")]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int id)