Add parameter using PagedList in record detail and page links

1

Does anyone know how to keep a parameter between the various pages of the PagedList?

The situation is this: I used Scope_Identity when creating a row in a table, since I made the Id of the created parameter item to another action , that other < in> action works with Partial View and PagedList.

The following is the code below:

// Código do método post de inserir a informação no bd

 [HttpPost]
    [ActionName("Create")]
    public ActionResult Create_POST(Produto produto, int Id)
    {
        if (ModelState.IsValid)
        {
            ProdutoBLL produtoBll = new ProdutoBLL();
            produto.Id = produtoBll.PesquisaId(produto);
            Session["IdProduto"] = produto.Id;
            return RedirectToAction("Catalogo", "Material", new { produto = produto.Id });
        }
        return View();
    }

// Código do método GET que utiliza o Id que acabou de ser criado.   

 [HttpGet]
    [Authorize(Roles = "Gerente,Funcionario")]
    [ActionName("Catalogo")]
    public ActionResult Catalogo(int produto, int? pag, int? mats)
    {
        material.produto = produto;
        return View(materialBll.BuscaMaterial().ToPagedList(pag ?? 1, 3));
    }

The problem is this: my parameter is not passed to the second page of PagedList onwards. On the first page everything works, in the others it gives error and my URL does not present the product parameter.

    
asked by anonymous 24.04.2017 / 04:36

1 answer

0

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>
    
24.04.2017 / 06:03