The PagedList documentation or the X.PagedList (the latest version) suggests something like this:
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { pag = page }) )
This is a common problem ( I wrote a course that I teach to solve it and more some others , by the way). The problem is not the paging itself, but the search parameters are not preserved when detailing the record or going to a different page.
To resolve, you need to implement two extensions : one for HtmlHelper
and another one for the UrlHelper
". These are:
HtmlHelperExtensions
public static class HtmlHelperExtensions
{
/// <summary>
///
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="linkText"></param>
/// <param name="action"></param>
/// <returns></returns>
public static string ActionQueryLink(this HtmlHelper htmlHelper,
string linkText, string action)
{
return ActionQueryLink(htmlHelper, linkText, action, null);
}
/// <summary>
///
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="linkText"></param>
/// <param name="action"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string ActionQueryLink(this HtmlHelper htmlHelper,
string linkText, string action, object routeValues)
{
var queryString =
htmlHelper.ViewContext.HttpContext.Request.QueryString;
var newRoute = routeValues == null
? htmlHelper.ViewContext.RouteData.Values
: new RouteValueDictionary(routeValues);
foreach (string key in queryString.Keys)
{
if (!newRoute.ContainsKey(key))
newRoute.Add(key, queryString[key]);
}
return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
htmlHelper.RouteCollection, linkText, null /* routeName */,
action, null, newRoute, null);
}
public static string ActionReferrerQuery(this HtmlHelper htmlHelper,
string linkText, string action, string controller, object routeValues)
{
var referrer = htmlHelper.ViewContext.HttpContext.Request.UrlReferrer;
if (referrer.Query == null) return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
htmlHelper.RouteCollection, linkText, "Default", action, controller, null, null);
var queryString = referrer.Query.Replace("?", "");
var newRoute = routeValues == null
? htmlHelper.ViewContext.RouteData.Values
: new RouteValueDictionary(routeValues);
if (!string.IsNullOrEmpty(queryString))
{
foreach (string key in queryString.Split('&'))
{
var keyValuePair = key.Split('=');
if (!newRoute.ContainsKey(keyValuePair[0]))
newRoute.Add(keyValuePair[0], keyValuePair[1]);
}
}
return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
htmlHelper.RouteCollection, linkText, "Default", action, controller, newRoute, null);
}
}
UrlHelperExtensions
public static class UrlHelperExtensions
{
public static string ActionQuery(this UrlHelper urlHelper,
string action, string controller)
{
return ActionQuery(urlHelper, action, controller, null);
}
public static string ActionQuery(this UrlHelper urlHelper,
string action, string controller, object routeValues)
{
var queryString =
urlHelper.RequestContext.HttpContext.Request.QueryString;
var newRoute = routeValues == null
? urlHelper.RequestContext.RouteData.Values
: new RouteValueDictionary(routeValues);
foreach (string key in queryString.Keys)
{
if (!newRoute.ContainsKey(key))
newRoute.Add(key, queryString[key]);
}
return UrlHelper.GenerateUrl("Default", action, controller, newRoute,
urlHelper.RouteCollection, urlHelper.RequestContext, true);
}
public static string ActionReferrerQuery(this UrlHelper urlHelper,
string action, string controller, object routeValues)
{
var referrer = urlHelper.RequestContext.HttpContext.Request.UrlReferrer;
if (referrer.Query == null) return UrlHelper.GenerateUrl("Default", action, controller, null,
urlHelper.RouteCollection, urlHelper.RequestContext, true);
var queryString = referrer.Query.Replace("?", "");
var newRoute = routeValues == null
? urlHelper.RequestContext.RouteData.Values
: new RouteValueDictionary(routeValues);
if (!string.IsNullOrEmpty(queryString))
{
foreach (string key in queryString.Split('&'))
{
var keyValuePair = key.Split('=');
if (!newRoute.ContainsKey(keyValuePair[0]))
newRoute.Add(keyValuePair[0], keyValuePair[1]);
}
}
return UrlHelper.GenerateUrl("Default", action, controller, newRoute,
urlHelper.RouteCollection, urlHelper.RequestContext, true);
}
}
Once you've done this, you can use the link like this:
@Html.PagedListPager((PagedList.IPagedList)Model, page => Url.ActionQuery("MinhaActionDePesquisa", "MeuController", new { pag = page }))
Parameters will be added automatically in pagination.
For Detail View , use the Back button:
<a href="@Url.ActionReferrerQuery("MinhaActionDePesquisa", "MeuController", null)">Voltar para Lista/Pesquisa</a>