[英]C# how to remove a current row from the table by button click
I am trying to remove a table row from the table:我正在尝试从表格中删除表格行:
<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>
How could I send the index of the row to the controller?如何将行的索引发送到控制器?
public ActionResult Remove()
{
Task.RemoveAt();
return RedirectToAction("Index");
}
The row where I pressed the button would get removed.我按下按钮的那一行将被删除。
Change view code to:将视图代码更改为:
<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>
Then, change Remove
method to:然后,将
Remove
方法更改为:
public ActionResult Remove(int id)
{
Task.RemoveAt(id);
return RedirectToAction("Index");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.