bootstrap adminlte教程4-4:文章管理-批量删除文件
后台管理文件列表已完成,我们下一步就要实现批量删除了。
思路:前台用checkbo
后台使用FormCollection进行读写
但我现在还真不会FormCollection只选取checkbox的值,我会把所有的东西全读出来,不过不怕,有一个分析,如是读不出数据则不执行删除
1.前台
@*@model IEnumerable<jsdhh2.Models.CmsContent>*@ @model PagedList.IPagedList<jsdhh2.Models.CmsContent> @using PagedList.Mvc; <link href="~/Content/PagedList.css" rel="stylesheet" /> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_AdminLayout.cshtml"; } <!-- Content Header (Page header) --> <section class="content-header"> <h1> 文章管理 <small>分为超管和本人</small> </h1> <ol class="breadcrumb"> <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li> <li class="active">Here</li> </ol> </section> <!--end Content Header (Page header) --> <!-- Main content --> <section class="content"> @using (Html.BeginForm("DelCms", "CmsContents", new { @class = "form-inline" }, FormMethod.Post)) { <div class="col-xs-12"> <div class=" "> <div class="panel panel-default"> <div class="panel-heading"> <a class="btn btn-primary" href="/CmsContents/Create">新建</a> <input type="submit" class="btn btn-warning" value="删除" /> </div> <div class="panel-body"> <div class="form-inline"> <div class="form-group"> 请选择栏目: @Html.DropDownList("SeachColumnString", new SelectList(ViewBag.drolistmenu, "Value", "Text"), "请选择") </div> <div class="form-group"> <div class="input-group col-md-12"> @Html.TextBox("SearchString", "", new { @class = "form-control", @Style = "display:inline" }) <span class="input-group-btn "> <input id="btnSearch" class="btn btn btn-primary" type="submit" value="搜索"> </span> </div> </div> </div> </div> </div> </div> <div class="box"> <div class="box-body table-responsive no-padding"> <table class="table"> <tr> <th> </th> <th> 标题 </th> <th>创建时间 </th> <th> 创建者 </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.CheckBox(item.Id, false) </td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.CreatTime, "DateTimeTemplate") </td> <td>@Html.DisplayFor(modelItem => item.CreatUser) </td> <td> @Html.ActionLink("修改", "Edit", new { id = item.Id }, new { @class = "btn btn-info" }) | @Html.ActionLink("阅读", "Details", new { id = item.Id }, new { @class = "btn btn-success" }) | @Html.ActionLink("删除", "Delete", new { id = item.Id }, new { @class = "btn btn-warning" }) </td> </tr> } </table> </div> </div><!-- /.box --> </div> } <!-- Main content --> Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("Index", new { page, SeachColumnString = ViewBag.SeachColumnString, SearchString = ViewBag.SearchString })) </section>
2.后端
// 批量删除文件功能 public ActionResult DelCms(FormCollection form) { //var cmscontents = db.CmsContents; //db.MenuRoles.RemoveRange(menuroles); var winnars = from x in form.AllKeys where form[x] != "false" select x; foreach (var id2 in winnars) { if (id2 != "selectAll") //全选的那个不算数。那个checkbox值要去掉 { var cmscontents = db.CmsContents.Find(id2); if (cmscontents!=null) { db.CmsContents.Remove(cmscontents); } } } db.SaveChanges(); return RedirectToAction("index", "CmsContents"); }
效果