这两天一直在搞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("");
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("");
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参数@;