【发布时间】:2012-02-13 17:54:08
【问题描述】:
我有一个用于显示项目列表的局部视图,我在几个不同的地方使用这个局部视图。在这个局部视图中,我使用了分页器 -
@Html.PagedListPager(Model, page => Url.Action(null, new { page = page }))
这会导致分页器显示我正在查看的任何操作和视图的页面 url。
问题是,在我的搜索页面上,我使用查询字符串作为搜索字符串,而 Url.Action 方法不包含现有的查询字符串参数。
我最终得到的是 /Search?page=3 而不是 /Search?s=bla&page=3
如何使用现有的查询字符串生成 url?
编辑:
这是我的代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(
"Search",
new SearchRoute("Search", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(
new { controller = "Search", action = "Index" })
});
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Call", action = "Index", id = UrlParameter.Optional }
);
}
public class SearchRoute : Route
{
public SearchRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
System.Diagnostics.Debug.WriteLine(Url);
if (HttpContext.Current != null)
{
string s = HttpContext.Current.Request.QueryString["s"];
if (!string.IsNullOrEmpty(s))
values.Add("s", s);
}
return base.GetVirtualPath(requestContext, values);
}
}
【问题讨论】:
标签: c# asp.net-mvc pagination