The documentation of Pagedlist or that of 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 some others, incidentally). The problem is not the pagination itself, but that the search parameters are not preserved by detailing the record or going to a different page.
To solve, you need to implement two Extensions: one for the HtmlHelper
and another to the UrlHelper
. Are they:
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);
}
}
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 automatically added in the pagination.
To the View detailing, use the Back button:
<a href="@Url.ActionReferrerQuery("MinhaActionDePesquisa", "MeuController", null)">Voltar para Lista/Pesquisa</a>
Just an alert, you are using a code that will generate bad performance, if you are bringing the general list of the table later using the
PagedList
for paging. You must use optimally in a specific method that will come from the database the already paginated data. You are using Entity Framework?– novic