简体   繁体   English

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

[英]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.

相关问题 通过单击 C# 中的按钮,从用户指定的表中删除一行 - Delete a row from a table, specified by the user, with the click of a button in C# 如何在单击 C# MVC 中的行中的按钮时将表行中的数据插入数据库 - How can i insert data from a table row into Database on clicking a button in the row in C# MVC "如何从按钮 C# 中删除面板" - How to remove panels from button C# 如何单击一个按钮,该按钮将从ListBox中删除一个项目,而该列表中包含C#中ListBox的信息? - How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#? 如何在按钮单击时从数据库中删除行 wpf c# sql - How to delete row from database on button click wpf c# sql 如何从jQuery json中的html表中删除当前行? - How to remove current row from a html table in jQuery json? 如何删除 DataGridView 最后一行 ButtonColumn 按钮 c#? - How to remove DataGridView last row ButtonColumn Button c#? C#:如何从以编程方式在Power Point中创建的表中删除第一行颜色 - C#: How to remove first row color from table created in power point programmatically 从C#表单按钮更新表行中的特定数据 - Updating specific data in table row from c# form button C#,单击按钮时datagridview中的行数 - C#, row count in datagridview on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM