0
我在表格中有一个Delete按钮,我尝试删除所选的Row。将Id传递给ActionMethod
的问题是,我总是在后的方法得到一个空ID
<div>
<table class="table">
<thead>
<tr>
<th>Role Name</th>
<th>Action</th>
</tr>
</thead>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@using (Html.BeginForm("Delete","Role", new { id = item.Id },FormMethod.Post))
{
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
</div>
}
</td>
</tr>
}
</table>
在我的控制器
// POST: Jobs/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult Delete(int id)
{
IdentityRole role = _context.Roles.Find(id);
_context.Roles.Remove(role);
_context.SaveChanges();
return RedirectToAction("Index");
}
任何时候,我点击按钮的id为null
向我们显示您的路线? – ramiramilu
'location.href'是一个GET。你不应该试图对'Delete()'方法进行GET调用。而且你的方法在任何情况下都用'[ValidateAntiForgeryToken]'来装饰(但你不传递该标记)。添加一个表单并在其中添加一个提交按钮(并且包含'@ Html.AntiForgeryToken()' –
@StephenMuecke我编辑了我的代码并添加了表单,但我仍然得到一个空ID –