【问题标题】:.Net Core 5 MVC not working delete action.Net Core 5 MVC 不工作删除操作
【发布时间】:2021-08-29 09:54:11
【问题描述】:

删除未触发的动作,即使其他所有动作都正常工作。 我得到 405 或 404 输出。当然,Id 不为空,很多不同的输入。 可能是什么问题?

这是我的控制器:-


    public IActionResult Delete(int? id){
            if (id == null || id == 0) {
                return NotFound();
            }
            var obj = _db.Expense.Find(id);
            if (obj == null){
                return NotFound();
            }
            return View(obj);
        }
        // POST Delete
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult DeletePost(int? id){
            var obj = _db.Expense.Find(id);
            if (obj == null){
                return NotFound();
            }
            _db.Expense.Remove(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

这是我的观点 没什么特别的,只是像往常一样调用 POST 动作。 :-



@model InAndOut.Models.Expense

<form method="post" asp-action="DeletePost">
    <input asp-for="Id" hidden />
    <div class="border p-3">
        <div class="form-group row">
            <h2 class="text-black-50 pl-3">Delete Expense</h2>
        </div>
        <div class="row">
            <div class="col-12">
               //here is form
                <div class="form-group row">
                    <div class="col-8 offset-2 row">
                        <div class="col">
                            <input type="submit" class="btn btn-danger w-75" value="Delete" />
                        </div>
                        <div class="col">
                            <a asp-action="Index" class="btn btn-success w-75">Back</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>


这是我默认的 MVC 路由:-


 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

【问题讨论】:

  • 数据库很可能没有这样的 id,它返回 null。或者您的 id 也无效或为空。用断点检查它
  • 删除按钮在哪里?

标签: c# asp.net-core-mvc crud .net-5


【解决方案1】:

我一直在做同样的教程,对我来说这些操作是有效的。请看一下我的代码。如果您还有其他问题,请与我联系。这是我的仓库:https://github.com/andreeapurta/AppointmentApplication

控制器:

            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult DeletePost(int? id)
            {
                var expense = dbContext.Expenses.Find(id);
                if (expense == null)
                {
                    return NotFound();
                }
                //get items from DB
                dbContext.Expenses.Remove(expense);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
    
            //Get - delete
            public IActionResult Delete(int? id)
            {
                if (id == null || id == 0)
                {
                    return NotFound();
                }
                //get items from DB
                var expense = dbContext.Expenses.Find(id);
                if (expense == null)
                {
                    return NotFound();
                }
    
                return View(expense);  // return the view with out object so we can display the info we are getting
            }

html:

<form method="post" asp-action="DeletePost">
    <input asp-for="Id" hidden/>
    <div class="border p-3">
        <div class="form-group row">
            <h2 class="text-black-50 pl-3">Delete Expense</h2>
        </div>
        <div class="row">
            <div class="col-12">
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="ExpenseName"></label>
                    </div>
                    <div class="col-4">
                        <label asp-for="Amount"></label>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-4">
                        <input asp-for="ExpenseName" disabled class="form-control" />
                    </div>
                    <div class="col-4">
                        <input asp-for="Amount" disabled class="form-control" />
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-8 offset-2 row">
                        <div class="col">
                            <input type="submit" class="btn btn-danger w-75" value="Delete" />
                        </div>
                        <div class="col">
                            <a asp-action="Index" class="btn btn-success w-75">Back</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
@*client validation*@
@section Scripts{
    @{
        <partial name="_ValidationScriptsPartial" />
    }
}

型号:

  public class Expense
    {
        public int Id { get; set; }

        [Required]
        public string ExpenseName { get; set; }

        [Required]
        [Range(1.00, 4000, ErrorMessage = "Amount must be greater than 0 and less than 4000")]
        public float Amount { get; set; }

        [DisplayName("Expense Type")]
        public int ExpenseCategoryId { get; set; }
        [ForeignKey("ExpenseCategoryId")]
        public virtual ExpenseCategory ExpenseCategory { get; set; }
    }

【讨论】:

    【解决方案2】:
    <form asp-controller="Write here name your controller" asp-action="DeletePost" method="post" >
        <div class="border p-3">
            <div class="form-group row">
                <h2 class="text-black-50 pl-3">Delete Expense</h2>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="form-group row">
                        <div class="col-8 offset-2 row">
                            <div class="col">
                                <input type="submit" class="btn btn-danger w-75" value="Delete" />
                            </div>
                            <div class="col">
                                <a asp-action="Index" class="btn btn success w-75">Back</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    

    【讨论】:

    • 尝试删除[ValidateAntiForgeryToken]我不确定是否有帮助,但请尝试
    • @Alex &lt;input type="number" asp-for="Id" hidden /&gt; 尝试添加“type="number"
    • @Alex 顺便说一句,如果我没记错的话,那么您显然不需要指定输入,因为 id 不应该出现在表单中,而是出现在查询字符串中。然后你以表格的形式传递它。尝试删除输入
    • 没用。这里的输入用于指定 ID,无论我们使用什么输入。从控制器传递的确切 ID,我相信
    • 尝试了你的新答案并输入了之前的答案,仍然没有......(
    【解决方案3】:

    这是在 VS2019 中测试的并且工作正常(假设你的 id 不为空)

    @model InAndOut.Models.Expense
    
    <form asp-action="DeletePost" asp-route-id="@Model.Id" method="post" >
     @Html.AntiForgeryToken()
    .....
    <input type="submit" class="btn btn-danger w-75" value="Delete" />
                            
    </form>
    
    

    或者你可以使用经典的 mvc 提交

    @model InAndOut.Models.Expense
    
    <form asp-action="DeletePost" method="post" >
     @Html.AntiForgeryToken()
    <input asp-for="@Model.Id" hidden />
    
    .....
    <input type="submit" class="btn btn-danger w-75" value="Delete" />
                            
    </form>
    

    以及针对此视图的操作

    [HttpPost]
     [ValidateAntiForgeryToken]
    public IActionResult DeletePost(Expense expense)
    

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2017-06-30
      • 2017-05-17
      • 2021-05-28
      相关资源
      最近更新 更多