Doubt with pagination in Asp.net

1

Does anyone know how to implement a paging or would I have an example using this architecture link I would like to use Linq's Skip () and Take ():

As was done in this example MVC asp.net page

But I'm having an error:

public IEnumerable<Cliente> ListarPagina(int paginaAtual, int itensPorPagina) { return contexto.Cliente.Skip((paginaAtual - 1) * itensPorPagina).Take(itensPorPagina).ToList(); } 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.Ceil(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<Cliente> ListarPagina(int paginaAtual, int itensPorPagina) { var clientes = contexto.Cliente; var totalClientes = cliente.Count(); var clientesDaPagina = cliente.Skip((paginaAtual - 1) * itensPorPagina).Take(itensPorPagina).ToList(); return new ListaPaginada<Cliente> (clientesDaPagina, totalClientes, itensPorPagina, paginaAtual); }     
asked by anonymous 04.12.2015 / 01:21

3 answers

0

I can not tell you why he used Math.ceil in that response code. I believe what you want is this class (Math) in C # which in turn has the method Ceiling .

Math.ceil you will find in JavaScript.

In the github link you posted he uses Math.cel only in his javascript (I'm putting this in the answer for you to see that it works on Javascritp). Check out this class of it .

    
04.12.2015 / 05:51
0

There is a library that could help you with this, it is called X.PagedList and in the project page in GitHub you will find some examples of use. The library is available from NuGet as well.

It may be that #

This question helps you with something,     

04.12.2015 / 06:25
0

After several attempts I decided to use the PagedList plugin, the final response is as follows:

Thanks to everyone for the contributions, within the architecture I'm studying, to be able to do practically all the necessary methods, more I count on the help of all for evolution.

        //http://localhost:1608/api/ApiGuiaCidade/consulta/paginacao/1/4
        [HttpGet]
        [Route("consulta/paginacao/{pagina:int}/{pagsize:int}")]
        public HttpResponseMessage ClientesPaginacao(int pagina,int pagsize)
        {
            try
            {
                int PageNumero = (pagina * 1);
                var tCliente = new ClienteAplicacao();
                var listarDeClientes = tCliente.ListarTodos();
                return Request.CreateResponse(HttpStatusCode.OK, listarDeClientes.ToPagedList(PageNumero, pagsize));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }
    
05.12.2015 / 00:39