asp.net core mvc 6没有找到该网址的网页:https://localhost:7168/Event/DeletePost

w1e3prcc  于 2023-04-13  发布在  .NET
关注(0)|答案(1)|浏览(161)

我不明白为什么我没有得到数据时,开始DeletePost行动方法,我得到了日期在删除方法,但没有得到在DeletePost有人能解释吗?

//Get - Delete
public IActionResult Delete(int? id)
{
    if (id == null || id == 0)
    {
        return NotFound();
    }
    var obj = _db.Event.Find(id);
    if (obj == null)
    {
        return NotFound();
    }
    return View(obj);
}

//Post - Delete
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int? id)
{
    var obj = _db.Event.Find(id);

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

这里是删除视图,我发送请求到DeletePost,不要不如果它帮助一些如何。但mby它帮助一些如何。我得到后的方法,并得到asp-action删除视图

<form method="post" asp-action="DeletePost">
    <input asp-for="Event_ID" hidden />
    <div class="border p-3">
        <div class="form-group row">
            <h2 class="text-info pl-3">Delete Event</h2>
        </div>
        <div class="row">
            <div class="col-8">
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="Event_Title"></label>
                    </div>
                    <div class="col-8">
                        <input asp-for="Event_Title" disabled class="form-control" />
                    </div>

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

                    </div>

                </div>
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="Event_ShortDesc"></label>
                    </div>
                    <div class="col-8">
                        <input asp-for="Event_ShortDesc" 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="fa-solid fa-right-to-bracket"></i>&nbsp;Back</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-4">
                @* Keep this empty *@
            </div>
        </div>
    </div>

</form>
lb3vh1jj

lb3vh1jj1#

使用asp-for="Event_ID",它将<input> HTML元素呈现为

<input id="Event_ID" name="Event_ID" hidden />

提交表单时,它将提交Event_ID值作为Event_ID参数。
但是您的Action需要id参数。

public IActionResult DeletePost(int? id)

因此,您需要通过在<input>元素中提供name属性来覆盖它。因此,这将传递EVENT_ID值作为id参数。

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

参考:The Input Tag Helper

相关问题