我有一個顯示項目列表的局部視圖,我在幾個不同的地方使用了這個局部視圖。這裏面局部視圖我使用分頁程序 -根據當前URL生成URL並維護QueryString
@Html.PagedListPager(Model, page => Url.Action(null, new { page = page }))
這將導致分頁程序顯示的網頁網址爲任何行動和查看我已經在看。
問題是,在我的搜索頁面上,我使用查詢字符串作爲搜索字符串,而Url.Action方法不包括現有查詢字符串參數。
而不是/搜索?S =喇嘛&頁= 3我結束了/搜索頁→= 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);
}
}
神奇。你達人。如果有其他人使用此項,請記住將呼叫路由添加到默認路由上方。 – NoPyGod 2012-01-19 01:44:44
嗯等一下..這可能不能正常工作 – NoPyGod 2012-01-19 01:57:06
它將查詢字符串放在我的搜索頁面上生成的所有url上。這可以修復嗎?我只需要搜索網址上的查詢字符串。 – NoPyGod 2012-01-19 02:00:38