PagedList: Data Paging with Ajax

1

I'm using PagedList with Ajax, but I'm not getting a page change because when I click the button that represents the page, the value that is going to the controller is null. I made the following structure:

View:

@model PagedList.IPagedList<MeuProjeto.Web.ViewModels.ClienteViewModel>
@using PagedList.Mvc;
...
<div id="ClientesTableDiv">
    @Html.Partial("_ClientesTable", Model)
</div>

@Html.PagedListPager(Model, pagina => Url.Action("Paginacao","Cliente", pagina ), 
       PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( 
          new AjaxOptions() { 
                              HttpMethod = "POST",
                              UpdateTargetId = "ClientesTableDiv" 
                            }))

Controller:

[HttpPost]
public PartialViewResult Paginacao(int? pagina)
{
    ...
    int numeroPagina = pagina ?? 1; // A pagina está vindo como nula.
    int tamanhoPagina = 5;

    return PartialView("_ClientesTable",
              clientesViewModel.ToPagedList(numeroPagina, tamanhoPagina));
}

Should the last line of the View code be changed?

    
asked by anonymous 18.12.2016 / 01:23

1 answer

2

There is an adjustment to be made here:

  • It is pagina put new {pagina} :
  • Instead of POST , put GET , because in this case it is link :
  

@Html.PagedListPager(Model, pagina => Url.Action("Paginacao","Cliente", new { pagina }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "ClientesTableDiv" }))

  • In the method also change verb to GET
[HttpGet]
public PartialViewResult Paginacao(int? pagina)
{
    ...
    int numeroPagina = pagina ?? 1; // A pagina está vindo como nula.
    int tamanhoPagina = 5;

    return PartialView("_ClientesTable",
        clientesViewModel.ToPagedList(numeroPagina, tamanhoPagina));
}
    
18.12.2016 / 14:55