gpt4 book ai didi

asp.net-mvc - 在检查复选框时删除表格行

转载 作者:行者123 更新时间:2023-12-04 17:41:07 25 4
gpt4 key购买 nike

我有包含数据的表。在每一行中都有一个复选框和一个复选框,用于选择标题处的所有复选框。
选中此复选框后,将从数据库表中删除相应的行。另外,在选中标题处的复选框时,将从数据库表中删除所有行。我如何实现此 asp.net mvc。

最佳答案

一如既往地从模型开始:

public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}

然后是一个 Controller :
public class HomeController : Controller
{
// TODO: Fetch this from a repository
private static List<ProductViewModel> _products = new List<ProductViewModel>
{
new ProductViewModel { Id = 1, Name = "Product 1" },
new ProductViewModel { Id = 2, Name = "Product 2" },
new ProductViewModel { Id = 3, Name = "Product 3" },
new ProductViewModel { Id = 4, Name = "Product 4" },
new ProductViewModel { Id = 5, Name = "Product 5" },
};

public ActionResult Index()
{
return View(_products);
}

[HttpPost]
public ActionResult Delete(IEnumerable<int> productIdsToDelete)
{
// TODO: Perform the delete from a repository
_products.RemoveAll(p => productIdsToDelete.Contains(p.Id));
return RedirectToAction("index");
}
}

最后是 Index.aspx看法:
<% using (Html.BeginForm("delete", "home", FormMethod.Post)) { %>

<table>
<thead>
<tr>
<th>Name</th>
<th>Select</th>
</tr>
</thead>
<tbody>
<%= Html.EditorForModel()%>
</tbody>
</table>

<input type="submit" value="Delete selected products" />

<% } %>

以及产品编辑器模板( ~/Views/Home/EditorTemplates/ProductViewModel.ascx ):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ToDD.Controllers.ProductViewModel>" %>
<tr>
<td>
<%: Model.Name %>
</td>
<td>
<input type="checkbox" name="productIdsToDelete" value="<%: Model.Id %>" />
</td>
</tr>

关于asp.net-mvc - 在检查复选框时删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3665218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com