Chinaunix首页 | 论坛 | 博客
  • 博客访问: 869103
  • 博文数量: 366
  • 博客积分: 10267
  • 博客等级: 上将
  • 技术积分: 4290
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:04
文章分类

全部博文(366)

文章存档

2012年(366)

分类: 系统运维

2012-04-22 19:59:39

这两天一直在搞MVC 2.0的查询后分页,分页控件用的MikePager。

刚开始用的MikePagerAjaxExtensions.cs,代码如下:

复制代码
1 using System; 2 using System.Text; 3 using System.Web.Routing; 4 5 namespace System.Web.Mvc 6 { 7 public static class MikePagerAjaxExtensions 8 { 9 #region MikePager 分页控件 10 11 public static string MikePager(this AjaxHelper html, PagedList data) 12 { 13 14 int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1; 15 int end = (data.TotalPage - start) > 10 ? start + 10 : data.TotalPage; 16 17 RouteValueDictionary vs = html.ViewContext.RouteData.Values; 18 var queryString = html.ViewContext.HttpContext.Request.QueryString; 19 foreach (string key in queryString.Keys) 20 vs[key] = queryString[key]; 21 22 var FormString = html.ViewContext.HttpContext.Request.Form; 23 foreach (string key in FormString.Keys) 24 vs[key] = FormString[key]; 25 26 string Formstring = FormString.ToString().Replace("&", ","); 27 28 //var url = html.ViewContext.HttpContext.Request.Url; 29 var url1 = html.ViewContext.HttpContext.Request.RawUrl; 30 var builder = new StringBuilder(); 31 builder.AppendFormat("
"); 32 33 //vs["pageSize"] = data.PageSize; 34 if (data.IsPreviousPage) 35 { 36 vs["pageIndex"] = 1; 37 38 builder.Append(Ajax.AjaxExtensions.ActionLink(html, "首页", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })); 39 40 vs["pageIndex"] = data.PageIndex - 1; 41 42 builder.Append(Ajax.AjaxExtensions.ActionLink(html, "上一页", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })); 43 44 } 45 46 for (int i = start; i <= end; i++) //前后各显示5个数字页码 47 { 48 vs["pageIndex"] = i; 49 50 if (i == data.PageIndex) 51 { 52 builder.Append("" + i.ToString() + ""); 53 } 54 else 55 { 56 builder.Append(Ajax.AjaxExtensions.ActionLink(html, i.ToString(), vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })); 57 } 58 } 59 60 if (data.IsNextPage) 61 { 62 vs["pageIndex"] = data.PageIndex + 1; 63 64 builder.Append(Ajax.AjaxExtensions.ActionLink(html, "下一页", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })); 65 66 vs["pageIndex"] = data.TotalPage; 67 68 builder.Append(Ajax.AjaxExtensions.ActionLink(html, "末页", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })); 69 70 } 71 builder.Append("每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "
"); 72 return builder.ToString(); 73 } 74 #endregion 75 76 } 77 }
复制代码

前台搜索的form为:

复制代码
1 <%using(Ajax.BeginForm(new AjaxOptions{})){ %> 2 <div> 3 4 <%=Html.TextBox("keyword")%> 5 <%=Html.DropDownList("input1", new List<SelectListItem>{new SelectListItem{Text=""},new SelectListItem{Text=""},new SelectListItem{Text=""}}) %> 6 <%=Html.DropDownList("pageSize", new List<SelectListItem> { new SelectListItem { Text = "" }, new SelectListItem { Text = "5",Value="5" }, new SelectListItem { Text = "10",Value="10" }, new SelectListItem { Text = "15",Value="15" }, new SelectListItem { Text = "20",Value="20" } })%> 7 <input id="pageIndex" name="pageIndex" type="hidden" value="pageIndex" /> 8 <input type="submit" onclick="$(function(){$.jBox.tip('正在处理...', 'loading');});" value="搜索"/> 9 div> 10 <%} %>
复制代码

点击后搜索成功,也可以分页,但是点击分页控件时,不能保留Url参数,譬如:选择性别***=“0”(男),页数pageSize=5,

点击分页控件查看下一页,本来的URL应该是 /Sys_Controllers/Index/System.Web.Mvc.UrlParameter/2?pageSize=5&***=0

但是实际点击后的Url为:/Sys_Controllers /Index/System.Web.Mvc.UrlParameter/2?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D

如果想保留Url参数,则需要修改MikePagerAjaxExtensions.cs中的

1 Ajax.AjaxExtensions.ActionLink(html, i.ToString(), vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })

 

为:

Ajax.AjaxExtensions.ActionLink(html, i.ToString(), vs["action"].ToString(), new{pageSize=vs["pageSize"],***=vs["***"]}, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }, new { onclick = "$(function(){$.jBox.tip('正在处理...', 'loading');});" })

 

暂时没有其他的解决方法。但是这种方法用在系统中很不方便,然后使用MikePagerHtmlExtensions.cs后可以完美的保留Url中的参数,代码为:

复制代码
1 using System.Text; 2 using System.Web.Routing; 3 4 namespace System.Web.Mvc 5 { 6 public static class MikePagerHtmlExtensions 7 { 8 9 #region MikePager 分页控件 10 11 public static string MikePager(this HtmlHelper html, PagedList data) 12 { 13 int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1; 14 int end = (data.TotalPage - start) > 10 ? start + 10 : data.TotalPage; 15 16 RouteValueDictionary vs = html.ViewContext.RouteData.Values; 17 18 var queryString = html.ViewContext.HttpContext.Request.QueryString; 19 foreach (string key in queryString.Keys) 20 //if (queryString[key] != null && !string.IsNullOrEmpty(key)) 21 vs[key] = queryString[key]; 22 23 var FormString = html.ViewContext.HttpContext.Request.Form; 24 foreach (string key in FormString.Keys) 25 vs[key] = FormString[key]; 26 27 var builder = new StringBuilder(); 28 builder.AppendFormat("
"); 29 30 if (data.IsPreviousPage) 31 { 32 vs["pageIndex"] = 1; 33 builder.Append(Html.LinkExtensions.ActionLink(html, "首页", vs["action"].ToString(), vs)); 34 vs["pageIndex"] = data.PageIndex - 1; 35 builder.Append(Html.LinkExtensions.ActionLink(html, "上一页", vs["action"].ToString(), vs)); 36 } 37 38 for (int i = start; i <= end; i++) //前后各显示5个数字页码 39 { 40 vs["pageIndex"] = i; 41 if (i == data.PageIndex) 42 { 43 builder.Append("" + i.ToString() + ""); 44 } 45 else 46 { 47 builder.Append(Html.LinkExtensions.ActionLink(html, i.ToString(), vs["action"].ToString(), vs)); 48 } 49 } 50 51 if (data.IsNextPage) 52 { 53 vs["pageIndex"] = data.PageIndex + 1; 54 builder.Append(Html.LinkExtensions.ActionLink(html, "下一页", vs["action"].ToString(), vs)); 55 vs["pageIndex"] = data.TotalPage; 56 builder.Append(Html.LinkExtensions.ActionLink(html, "末页", vs["action"].ToString(), vs)); 57 } 58 builder.Append("每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "
"); 59 return builder.ToString(); 60 } 61 #endregion 62 63 } 64 }
复制代码

 

对了,我的Global.asax.cs代码是:

复制代码
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 2 3 routes.MapRoute( 4 "Root", 5 "", 6 new { controller = "Index", action = "Index" }, 7 new string[] { "Web.Controllers" } 8 ); 9 10 routes.MapRoute( 11 "Default", // Route name 12 "{controller}.aspx/{action}/{id}/{pageIndex}", // URL with parameters 13 new { controller = "Index", action = "Index", id = UrlParameter.Optional, pageIndex = UrlParameter.Optional }, // Parameter defaults 14 new string[] { "Web.Controllers" } 15 );
复制代码

 

上面都是这两天搞的东西,对于ajax的MikePager我只能用到那种方法,如果那位看到这篇文章的大哥大姐有更好的方法,请贴出来共享一下,目前我用了html方式的MikePager可以保留Url参数@;

阅读(1724) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~