我正在尝试从表格中删除表格行:
<tbody>
@foreach (var task in Model)
{
<tr>
<td>
@task.time
</td>
<td>
@task.descripiton
</td>
<td>
@task.duedo
</td>
<td>
<form action="@Url.Action("Remove")" method="POST"><input type="submit" value="Delete" /></form>
</td>
</tr>
}
</tbody>
如何将行的索引发送给控制器?
public ActionResult Remove()
{
Task.RemoveAt();
return RedirectToAction("Index");
}
我按下按钮的行将被删除。
答案 0 :(得分:2)
将查看代码更改为:
<tbody>
@{
var idx = 0;
foreach (var task in Model)
{
<tr>
<td>
@task.time
</td>
<td>
@task.descripiton
</td>
<td>
@task.duedo
</td>
<td>
<form action="@Url.Action("Remove", "YourControllerName", new { id = idx++ })" method="POST">
<input type="submit" value="Delete" />
</form>
</td>
</tr>
}
}
</tbody>
然后,将Remove
方法更改为:
public ActionResult Remove(int id)
{
Task.RemoveAt(id);
return RedirectToAction("Index");
}