Doubt with pagination in Asp.net

Asked

Viewed 237 times

1

Does anyone know how to implement a pagination or would have an example using this architecture https://github.com/cleytonferrari/PadraoDeRepositorio/tree/master/TISelvagem I would like to use the Skip() and Take() from Latin:

As was done in this example MVC Asp.net paging

but I’m having a mistake:

inserir a descrição da imagem aqui

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);
}
  • 1

    This I do not know, I use this one, it serves me 100%, I have 400 thousand records. https://www.nuget.org/packages/PagedList.Mvc/

  • I would like to learn to do is not to use ready-made things, not that I am against each use what you think best, but I would like to learn to do

  • So put what you’ve done so we can debate the problems and doubts. The question is quite vague, actually not hard to do no, but everything will depend on the technologies you chose

  • All right, there you go!

3 answers

0

I can’t 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.

On the github link you posted it only uses Math.cel in his javascript (I’m putting this in the answer so you can see that it works in Javascritp). Look at that class his.

  • If possible I would like to learn to do this without needing to use anything ready, it is not a question of inventing the wheel, but for me avoid to the maximum using things ready can avoid possible headaches with changes.

  • Actually I didn’t put anything ready. rsrs, not using Math is rewriting the microsoft framework

  • 1

    I ended up typing wrong anyway. It was supposed to be the Math.Ceiling. I already corrected in the example. Rounding there is to calculate the number of total pages to be displayed. Rounded up with the Ceiling because a list of 11 records and 10-by-10 pagination needs to be 2 pages. If dividing 11 by 10 and not rounding up will only have 1 page, which is not correct for the case, because we would have to have one more page with only one record.

  • Good that corrected the error. Have more questions? If corrected the error does not fail to mark the answer.

0

  • If possible I would like to learn to do this without needing to use anything ready, it is not a question of inventing the wheel, but for me avoid to the maximum using things ready can avoid possible headaches with changes.

  • @itasouza The ideal is for you not to reinvent the wheel, but if you are looking to reinvent the same way, take a look at the linked question in my answer, there I did a pagination check in hand.

0

After several attempts I decided to use the Pagedlist Plugin, the final answer was this way:

I thank everyone for the contributions, within the architecture I am studying, already able to make practically all the necessary methods, more 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);
            }
        }

Browser other questions tagged

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