Just for the record, as an alternative for those who want to learn how to do it in the hand, in a simple way (not that the PagedList
not be, just does not reveal what it does behind the scenes), just create a method that accepts the current page and the amount of items per page and use the methods Skip()
and Take()
of the Ligurian:
public IEnumerable<Clientes> ListarPagina(int paginaAtual, int itensPorPagina)
{
return contexto.Clientes.Skip((paginaAtual - 1) * itensPorPagina).Take(itensPorPagina).ToList();
}
If you want to assemble the pagination links and have a ViewModel
specific to what you need to work with, you can modify the case a little and create a generic template for your paged lists, which accepts the total records, the current page, the amount of items per page and the collection of items:
public class ListaPaginada<T>
{
public int TotalItens { get; private set; }
public int ItensPorPagina { get; private set; }
public int PaginaAtual { get; private set; }
public int TotalPaginas
{
get { return Math.Ceiling(TotalItens / ItensPorPagina); }
}
public List<T> Itens { get; private set; }
public ListaPaginada(List<T> itens, int totalItens, int itensPorPagina, int paginaAtual)
{
this.Itens = itens;
this.TotalItens = totalItens;
this.ItensPorPagina = itensPorPagina;
this.PaginaAtual = paginaAtual;
}
}
public ListaPaginada<Clientes> ListarPagina(int paginaAtual, int itensPorPagina)
{
var clientes = contexto.Clientes;
var totalClientes = clientes.Count();
var clientesDaPagina = clientes.Skip((paginaAtual - 1) * itensPorPagina).Take(itensPorPagina).ToList();
return new ListaPaginada<Clientes> (clientesDaPagina, totalClientes, itensPorPagina, paginaAtual);
}
In your view, you will have a single object with the properties to make the loops and generate the pagination and list as you wish:
@model ListaPaginada<Clientes>
if (Model.TotalItens > 0)
{
foreach(var item in Model.Itens)
{
<li>@item.Nome</li>
}
if (Model.TotalPaginas > 1)
{
<div class="paginacao">
@for(var i = 1; i < Model.TotalPaginas; i++)
{
Url.Action("Index", new { pagina = i })
}
</div>
}
}
else
{
<p>Nenhum item disponível no momento!</p>
}
I do not recommend replacing the component, I just present a way to do the same in a personalized way, with good performance and, probably what the component does behind the scenes, without the unnecessary additions.
If you can do it in Controller !!! that’s the question, or you want example of how to do it... In the view is not indicated because of performance
– user6026
But the slowness is not in context? Isn’t it a slow process I bring from the bank all, and then filter through the controller? The correct thing would not be for me to bring already filtered by the bank.
– Diego Zanardo
So @Diegozanardo, really, so I asked you if you want an example, because the way you did giving a Tolist there iron the performance issue has to be otherwise so you just bring what you need and mount the pagination. Your question did not ask for example, just clarification!!!
– user6026
If you could give me an example it would be good. I edited the question!
– Diego Zanardo
you will have to suit your model!
– user6026
The performance will depend on your needs. For example bank paging may be more performative but will give you more work to sort/filter because in this case you would have to make a call to the BD whenever the user navigates the pages, reorder or filter. Paging in the controller is a middle ground and the worst performance "usually" is paging in the view but hence neither a call to the controller you need when browsing, sorting, filtering
– jean