接上篇mvc3 分页Helper.
带查询的分页Helper是在上一篇分页的基础上来的。下面看代码:
首先,在System.Web.Mvc命名空间下的自定义类HtmlPage下面添加一个用于处理“查询字典”的方法UrlGetParameter。
class="code_img_closed" src="/Upload/Images/2013072921/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('90f1284f-8c7d-4483-bf85-55eb4bebdc09',event)" src="/Upload/Images/2013072921/2B1B950FA3DF188F.gif" alt="" />1 /// <summary> 2 /// 根据查询字典,拼写查询参数 3 /// </summary> 4 /// <param name="parameters"></param> 5 /// <returns></returns> 6 public static string UrlGetParameter(Dictionary<string,string> parameters) 7 { 8 if (parameters != null && parameters.Count > 0) 9 { 10 StringBuilder sb = new StringBuilder(); 11 foreach (var item in parameters) 12 { 13 sb.Append("&"+item.Key.ToLower()+"="+item.Value); 14 } 15 return sb.ToString(); 16 } 17 else 18 { 19 return ""; 20 } 21 }UrlGetParameter方法
然后,修改HtmlPage类下面的ShowPageNavigate方法如下:
1 public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount,Dictionary<string,string> parameters=null) 2 { 3 var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; 4 pageSize = pageSize == 0 ? 3 : pageSize; 5 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数 6 string searchCode = string.Empty; 7 if (parameters!=null) 8 { 9 searchCode = UrlGetParameter(parameters); 10 } 11 var output = new StringBuilder(); 12 if (totalPages > 1) 13 { 14 output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}{2}'>首页</a> ", redirectTo, pageSize,searchCode); 15 if (currentPage > 1) 16 {//处理上一页的连接 17 output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}{3}'>上一页</a> ", redirectTo, currentPage - 1, pageSize,searchCode); 18 } 19 20 output.Append(" "); 21 int currint = 5; 22 for (int i = 0; i <= 10; i++) 23 {//一共最多显示10个页码,前面5个,后面5个 24 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages) 25 { 26 if (currint == i) 27 {//当前页处理 28 output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}{3}'>{4}</a> ", redirectTo, currentPage, pageSize, searchCode, currentPage); 29 } 30 else 31 {//一般页处理 32 output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}{4}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint,searchCode); 33 } 34 } 35 output.Append(" "); 36 } 37 if (currentPage < totalPages) 38 {//处理下一页的链接 39 output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}{3}'>下一页</a> ", redirectTo, currentPage + 1, pageSize,searchCode); 40 } 41 42 output.Append(" "); 43 if (currentPage != totalPages) 44 { 45 output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}{3}'>末页</a> ", redirectTo, totalPages, pageSize,searchCode); 46 } 47 output.Append(" "); 48 } 49 output.AppendFormat("<label>第{0}页 / 共{1}页</label>", currentPage, totalPages);//这个统计加不加都行 50 51 return new HtmlString(output.ToString()); 52 }ShowPageNavigate方法
其次,还要将"查询字典"属性添加到PageInfo字典中。
1 public class PagerInfo 2 { 3 public int RecordCount { get; set; } 4 5 public int CurrentPageIndex { get; set; } 6 7 public int PageSize { get; set; } 8 //放置查询参数 9 public Dictionary<string, string> SearchParameter { get; set; } 10 }PageInfo类
最后,就要写View和Controller里的内容的。先看Controller的代码,添加一个用于回发的Action即(Index),把get,post的Action都写出来吧。
1 //get /News/Index/? 2 public ActionResult Index(int? pageSize, int? pageIndex, string pauthor, string ptitle) 3 { 4 Dictionary<string, string> pagerParamers = new Dictionary<string, string>(); 5 int pageIndex1 = pageIndex ?? 1; 6 int pageSize1 = pageSize ?? 5; 7 //从数据库在取得数据,并返回总记录数 8 var temp = newsSer.LoadEntities(c => c.del == false).AsQueryable(); 9 PagerInfo pager = new PagerInfo(); 10 pager.CurrentPageIndex = pageIndex1; 11 pager.PageSize = pageSize1; 12 if (!string.IsNullOrEmpty(pauthor)) 13 { 14 pagerParamers.Add("pauthor", pauthor); 15 temp = temp.Where(c => c.author.Contains(pauthor)).AsQueryable(); 16 } 17 if (!string.IsNullOrEmpty(ptitle)) 18 { 19 pagerParamers.Add("ptitle", ptitle); 20 temp=temp.Where(c => c.title.Contains(ptitle)).AsQueryable(); 21 } 22 pager.RecordCount = temp.Count(); 23 pager.SearchParameter = pagerParamers; 24 temp=temp.OrderByDescending(c => c.id).Skip((pageIndex1 - 1) * pageSize1).Take(pageSize1).AsQueryable(); 25 PagerQuery<PagerInfo, IQueryable<news>> query = new PagerQuery<PagerInfo, IQueryable<news>>(pager, temp); 26 return View(query); 27 } 28 29 //Post /News/Index/? 30 [HttpPost] 31 public ActionResult Index(FormCollection form) 32 { 33 try 34 { 35 string pauthor = form["pauthor"]; 36 string ptitle = form["ptitle"]; 37 Dictionary<string, string> pagerParams = new Dictionary<string, string>(); 38 int pageIndex = 1; 39 int pageSize = 5; 40 PagerInfo pager = new PagerInfo(); 41 pager.CurrentPageIndex = pageIndex; 42 pager.PageSize = pageSize; 43 //从数据库在取得数据,并返回总记录数 44 var temp = newsSer.LoadEntities(c => c.author.Contains(pauthor)).AsQueryable(); 45 if (!string.IsNullOrEmpty(pauthor)) 46 { 47 pagerParams.Add("pauthor", pauthor); 48 temp = temp.Where(c => c.author.Contains(pauthor) && c.del == false).AsQueryable(); 49 } 50 if (!string.IsNullOrEmpty(ptitle)) 51 { 52 pagerParams.Add("ptitle", ptitle); 53 temp = temp.Where(c => c.title.Contains(ptitle)).AsQueryable(); 54 } 55 pager.RecordCount = temp.Count(); 56 pager.SearchParameter = pagerParams; 57 temp = temp.OrderBy(c => c.id).Skip((pageIndex - 1) * pageSize).Take(pageSize); 58 PagerQuery<PagerInfo, IQueryable<news>> query = new PagerQuery<PagerInfo, IQueryable<news>>(pager, temp); 59 return View(query); 60 } 61 catch 62 { 63 return View(); 64 } 65 }Action (Index)
接下来,看一下View中的代码:
1 @using (Html.BeginForm("Index", "News", FormMethod.Post, new { @class = "well" })) 2 { 3 <input type="text" id="pauthor" class="search-query" name="pauthor" placeholder="作者" /> 4 <input type="text" id="ptitle" class="search-query" name="ptitle" placeholder="标题" /> 5 <input type="submit" class="btn" value="查询" /> 6 } 7 <table style="margin-top: 10px;"> 8 <thead> 9 <tr> 10 <th width="25"> 11 <input class="select-all" name="" type="checkbox" value="" /> 12 </th> 13 <th> 14 作者 15 </th> 16 <th> 17 新闻标题 18 </th> 19 <th> 20 创建时间 21 </th> 22 <th> 23 操作 24 </th> 25 </tr> 26 </thead> 27 <tbody> 28 @foreach (var item in Model.EntityList) 29 { 30 <tr> 31 <td class="checkBox"> 32 <input name="ids[]" type="checkbox" value="" /> 33 </td> 34 <td> 35 @item.author 36 </td> 37 <td> 38 @item.title 39 </td> 40 <td> 41 @item.ctime 42 </td> 43 <td> 44 @Html.ActionLink("编辑", "Edit", new { id = item.id }) | 45 @Html.ActionLink("删除", "Delete", new { id = item.id }) 46 </td> 47 </tr> 48 } 49 @*分页*@ 50 <tr class=""> 51 <td colspan="5" align="center" class="paginator"> 52 <span> 53 @Html.ShowPageNavigate(Model.Pager.CurrentPageIndex, Model.Pager.PageSize, Model.Pager.RecordCount, Model.Pager.SearchParameter) 54 </span> 55 </td> 56 </tr> 57 </tbody> 58 </table>View Code
分页的样式见上一节"MVC# 分页Helper",查询表单的样式,就不再列出了。
最后再补充一下,如果在查询时依然想要使用以前的PageSize(当然了,这里的PageSize是固定的),可以在进行请求时将pageSize保存在ViewBag中,然后在View中将它保存在表单的隐藏域中,这样就可以使用设置好的pageSize了。(页面跳转、上n页、下n页,这里就不列出来了。)
最终的效果图:
ok.大功告成!