2014.4.17 更新:
[OutputCache(Duration = 300)] //condition是条件,page是当前页面 public ActionResult Index(string condition = "", int page = 1) { if (condition.ToLower() != "all") condition = " category='" + Server.HtmlDecode(condition.Replace("'", "")) + "' "; else condition = ""; //ToPagedList就是修改的MVCpager方法,参数:当前页号,分页大小,总记录数量 //FindAllByPage是自己实现的分页方法,根据条件只取当前页面的数据 PagedList<comnotices> notices = mgr.FindAllByPage(((page - 1) * 20).ToString(), "20", "", condition, 11).ToPagedList(page, 20, int.Parse(mgr.GetTotalCount("", condition))); if (Request.IsAjaxRequest()) return PartialView("NewsAjaxList", notices); return View(notices); }
主页面:
用户控件页面:
Controller:
public ActionResult Manage(int? page) { //var list = _agentManager.GetAgentList(); //var pageList = new PagedList<Agent>(list, (page ?? 1), ConfigFile.PerPageCount); //ViewData["pcount"] = list.Count; //ViewData["AdminId"] = AdminId; //ViewData["Actor"] = Actor; //return View(pageList);// return View(); } public ActionResult ManageSearch(int? page, FormCollection form) { DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now; var list = _agentManager.GetAgentList(); if (!string.IsNullOrEmpty(form["AgentId"])) { int agentId = 0; int.TryParse(form["AgentId"].Trim(), out agentId); list = list.Where(p => p.Id == agentId).ToList(); } if (!string.IsNullOrEmpty(form["AgentName"])) { list = list.Where(p => p.FullCompanyName.Contains(form["AgentName"].Trim())).ToList(); } if (!string.IsNullOrEmpty(form["StartTime"])) { startTime = Convert.ToDateTime(form["StartTime"]); list = list.Where(p => p.CreateTime >= startTime).ToList(); } if (!string.IsNullOrEmpty(form["EndTime"])) { endTime = Convert.ToDateTime(form["EndTime"]); //if (endTime == startTime) // endTime = endTime.AddDays(1); endTime = endTime.AddDays(1); list = list.Where(p => p.CreateTime <= endTime).ToList(); } if (!string.IsNullOrEmpty(form["ShortAgentName"])) { list = list.Where(p => p.ShortCompanyName.Contains(form["ShortAgentName"].Trim())).ToList(); } if (!string.IsNullOrEmpty(form["SaleMan"])) { list = list.Where(p => p.SaleMan.Contains(form["SaleMan"].Trim())).ToList(); } if (!string.IsNullOrEmpty(form["MinMoney"])) { int minMoney = 0; int.TryParse(form["MinMoney"], out minMoney); list = list.Where(p => p.Balance >= minMoney).ToList(); } if (!string.IsNullOrEmpty(form["MaxMoney"])) { int maxMoney = 0; int.TryParse(form["MaxMoney"], out maxMoney); list = list.Where(p => p.Balance <= maxMoney).ToList(); } var pageList = new PagedList<Agent>(list, page ?? 1, ConfigFile.PerPageCount); ViewData["pcount"] = list.Count; ViewData["AdminId"] = AdminId; ViewData["Actor"] = Actor; return PartialView("ManagePageList", pageList); }
重点:用jquery的ajax方法去服务器端取数据,然后把取出的数据显示到页面指定的分页数据处。
所以,用用户控件的return PartialView() 方法可以呈现部分视图到view. (即页面上没有<html><title><body><js>这样的标签的数据部分)。
但是这样还的建立一个部分视图的view页面,这时我想如果不建立数据视图页面,将整个当前页面再次载入一边是不是也可以?于是测试了一下,真的可以:
可以整体页面无刷新的分页雏形(不正规,因为这样<html>这样的头标签又下载了一次,加载到页面上。):
</body>
OK,成功!做成htmlhelper类:
/// <summary> /// Ajax数字分页(基于jQuery1.4 请自己加载)2010-11-13 /// </summary> /// <param name="helper">HtmlHelper</param> /// <param name="url">分页的URL(默认为本页)</param> /// <param name="pageList">IPagedList分页对象数据</param> /// <param name="pageLength">数字页码的长度</param> /// <returns></returns> public static string PagerHtmlBuilderForAjax(this HtmlHelper helper, string url, IPageList pageList, int pageLength) { var sb = new StringBuilder(); string str = PagerHtmlBuilder(helper, url, pageList, pageLength); sb.Append(str); //填充完毕 sb.Append("<script language=\"javascript\" type=\"text/javascript\">"); sb.Append("$(document).ready(function () {"); sb.Append("$(\"div.pager a\").click(function () {"); sb.Append("var tourl = $(this).attr(\"href\");"); sb.Append("$.get(tourl, function (data) {$(\"body\").empty().html(data);});"); sb.Append("return false;"); sb.Append("});"); sb.Append("});"); sb.Append("</script>"); return sb.ToString(); }
附:
mvc 数字分页展示类
/// <summary> /// 数字分页Paging with Numeric Pages /// </summary> /// <param name="helper"></param> /// <param name="url">分页的URL</param> /// <param name="pageList">IPagedList分页对象数据</param> /// <param name="pageLength">数字页码的长度</param> /// <returns></returns> public static string PagerHtmlBuilder(this HtmlHelper helper, string url, IPageList pageList, int pageLength) { var sb = new StringBuilder(); sb.Append("<div class=\"pager\">"); sb.Append(string.Format("共{1}页/{0}条记录", pageList.TotalCount, pageList.TotalPages)); string linkFormat = ""; pageLength = (pageLength > pageList.TotalPages) ? pageList.TotalPages : pageLength; if (url.IndexOf('?') > 0) //有参数 (如:?sid=6、?sid=3&c=1、?page=1、?page=1&c=2、?t=3&page=2) { if (url.IndexOf("page=") > 0) //有page参数 { //page参数后还有其他参数 (如:?page=2&type=2或?type=2&page=2&t=1) if (url.Substring(url.IndexOf("page=")).IndexOf("&") > 0) { var forward = url.Substring(0, url.IndexOf("page=")); //page前的部分 var endpar = url.Substring(url.LastIndexOf("page=") + 5); //page后的参数部分 url = forward + endpar.Substring(endpar.IndexOf("&") + 5);//去掉page后的部分 linkFormat = "<a href=\"{0}&page={1}\">{2}</a> "; } else //page参数后无其他参数 (如:?page=1、?type=2&page=2) { url = url.Remove(url.LastIndexOf("page=")); linkFormat = "<a href=\"{0}page={1}\">{2}</a> "; } } else //无page参数 (如:?type=2) { linkFormat = "<a href=\"{0}&page={1}\">{2}</a> "; } } else //无参数 { linkFormat = "<a href=\"{0}?page={1}\">{2}</a> "; } sb.Append(string.Format(linkFormat, url, "1", "首页"));//填充链接 if (pageList.IsPreviousPage) { sb.Append(string.Format(linkFormat, url, (pageList.PageIndex - 1), "上一页")); } int cpage = Convert.ToInt32(HttpContext.Current.Request["page"] ?? "1"); for (int j = 0, i = ((cpage - pageLength / 2 > 0) ? cpage - pageLength / 2 : 1); i <= pageList.TotalPages && j < pageLength; i++, j++) { if (i == cpage) { sb.Append("<span class=\"current\">" + i + "</span>"); } else { sb.Append(string.Format(linkFormat, url, i, i)); } } if (pageList.IsNextPage) { sb.Append(string.Format(linkFormat, url, (pageList.PageIndex + 1), "下一页")); } sb.Append(string.Format(linkFormat, url, pageList.TotalPages, "末页")); sb.Append("</div>"); return sb.ToString(); }