我不明白为什么我没有得到数据时,开始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> Back</a>
</div>
</div>
</div>
</div>
<div class="col-4">
@* Keep this empty *@
</div>
</div>
</div>
</form>
1条答案
按热度按时间lb3vh1jj1#
使用
asp-for="Event_ID"
,它将<input>
HTML元素呈现为提交表单时,它将提交
Event_ID
值作为Event_ID
参数。但是您的Action需要
id
参数。因此,您需要通过在
<input>
元素中提供name
属性来覆盖它。因此,这将传递EVENT_ID
值作为id
参数。参考:The Input Tag Helper