C#如何通过单击按钮从表中删除当前行

时间:2019-03-08 18:50:52

标签: c#

我正在尝试从表格中删除表格行:

<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");
}

我按下按钮的行将被删除。

1 个答案:

答案 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");
}