在WebGrid中做 批量删除操作

时间:2023-03-08 17:51:00
在WebGrid中做 批量删除操作

一般的MVC WebGrid都是在每一行中加入 Edit Detail Delete 这些Link 去对每条记录去单独操作。 稍微研究了一下总结一个 做批量删除的办法。

1. 首先是在WebGrid中加入一列CheckBox代码如下

       grid.Column(header: " ",
format: @<text><input class="check-box" id="chkbox" name="chkbox" type="checkbox" value="@item.CurrencyNo"/></text>),

2. 需要批量删除的需要在一个Form里 如果需要JS提交form要为form指定id

@using (Html.BeginForm("PreferredCurrency", "SysConfig", FormMethod.Post, new { id = "PreferredCurrency" }))

3. submit

js

      function deleteItems() {
document.getElementById("PreferredCurrency").submit();
}

button

<input type="submit" value="delete" />

4. Control 中的代码

        [HttpPost]
public ActionResult PreferredCurrency(FormCollection frm)
{
var ids = frm.GetValues("chkbox"); foreach(var id in ids)
{
//......................
    //to delete by id;
}
return View();
}