Add parameter using Pagedlist in logging detail and page links

Asked

Viewed 180 times

1

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

The situation is as follows: I used the Scope_Identity when creating a row in a table, because I did the Id of the parameter created item for another action, that other action works with Partial View and Pagedlist.

Follow 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. In the first page everything works, in the others it gives error and my URL does not present the parameter product.

  • 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?

1 answer

0

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.