.Net Core 5 MVC 不工作删除操作

.Net Core 5 MVC not working delete action

删除操作未触发,尽管其他所有操作都运行良好。 我得到 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?}");
            });

<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>

这已经在 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)

我一直在做同样的教程,对我来说这些动作很有效。请看看我的代码。如果您还有其他问题,请与我联系。这是我的回购协议: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; }
    }