bootstrap adminlte教程4-3:文章管理界面实现搜索功能
先上效果图
1.首先更改文章的index()前台
@model IEnumerable<jsdhh2.Models.CmsContent> @{ 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"> <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> </div> <div class="panel-body"> <form 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> </form> </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> </tr> @foreach (var item in Model) { <tr> <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 --> </section>
后台:注意引进来的二个变量,一定要和前台的那二个值一模一样。同时我取消了延迟加载。
//public async Task<ActionResult> Index(string SeachColumnString, string SearchString) public ActionResult Index(string SeachColumnString, string SearchString) { ViewBag.drolistmenu =db.CmsColumns.Select(g => new SelectListItem //这个是生成前台的选择栏目的下拉 { Text = g.Name, Value = g.Id.ToString(), Selected = false }); var seachindex = from w in db.CmsContents.OrderByDescending(c => c.CreatTime) //取出所有的数据并按日期倒序 select w; if (!string.IsNullOrEmpty(SeachColumnString)) //先看栏目是否为空 { int a1 = int.Parse(SeachColumnString); //传进的值 转换为int,因为我的栏目ID是int if (!string.IsNullOrEmpty(SearchString)) //查看是否输入了:标题内容 { seachindex = seachindex.Where(x => x.ColumnId == a1).Where(y =>y.Title.Contains(SearchString)); } else ///这只有栏目有值,未输入内容的查询 { seachindex = seachindex.Where(x => x.ColumnId == a1); } } else //栏目空,则只看是否输入了内容。 { if (!string.IsNullOrEmpty(SearchString)) { seachindex = seachindex.Where(x => x.Title.Contains(SearchString)); } } return View(seachindex); } // GET: CmsContents/Details/5
完成。