C# 实现批量删除功能
<input type="submit" class="btn btn-info" onclick="DelAll()" value="批量删除" />
<table class="table table-striped" id="table"> <tr> <td><input id="cb_selAll" type="checkbox" /></td> <td>ID</td> </tr> <tbody id="tb_body"> @foreach (var item in Model) { <tr> <td><input type="checkbox" value="@item.id" class="cb_fruit" /></td> <td id="id">@item.id</td> </tr> } </tbody> </table>
<script> $(function () { $("#cb_selAll").change(function () { $("#tb_body :checkbox").prop("checked", this.checked); }); }); function DelAll() { var id = []; $("#tb_body :checkbox:checked").each(function () { id.push($(this).val()); }); $.ajax({ url: "/Employees/DelAll", type: "GET", data: { id: id.join(",") }, success: function (data) { if (data > 0) { alert("成功!"); $("#tb_body :checkbox:checked").parent().parent().remove(); location.reload(); } else { alert("失败!"); } }, error: function () { alert("系统繁忙!"); } }); } </script>
/// <summary> /// DelAll /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] public int DelAll(string id = "0") { string[] datalist = id.Split(',');//定义数组 List<string> list = new List<string>(); foreach (var item in datalist) { string data = "'" + item.ToString() + "'"; list.Add(data); } string sql = string.Format("delete from Table where Id in(" + string.Join(",", list) + ")", list); return db.ExecuteNonQuery(sql); }