https://github.com/troygoode/PagedList or https://github.com/kpi-ua/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 ( http://www.codingcraft.com.br/tag/cursos-em-asp-net-mvc/ , indeed). The problem is not the page itself, but that the search parameters are not preserved when detailing the registry or going to a different page. To solve, you need to implement two extensions: one for the https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper(v=vs.118).aspx and another to the https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper(v=vs.118).aspx . They are:HtmlHelperExtensionspublic 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);
}
}
UrlHelperExtensionspublic 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);
}
}
Made this, you can use the link like this:@Html.PagedListPager((PagedList.IPagedList)Model, page => Url.ActionQuery("MinhaActionDePesquisa", "MeuController", new { pag = page }))
The parameters will be automatically added to the page. To the View for details, use the Back button:<a href="@Url.ActionReferrerQuery("MinhaActionDePesquisa", "MeuController", null)">Voltar para Lista/Pesquisa</a>