Web Paging with C#

Asked

Viewed 488 times

2

I’m building an administrative application, and in this application I have a message page. I am currently bringing the last 20 messages.

I already have the screen ready to receive this messages, but my problem is in making the logic of the button that show the next 20 messages, and so consecutively.

//Mensagens Enviadas
        List<Mensagem> mensagens = this.mensagemServico.GetMany(l => l.LojaId == loja.LojaId && l.OrigemId != 0).OrderByDescending(l => l.DataEnvio).Take(20).ToList();
        mensagemModel.Mensagens = mensagens;

        return View(mensagemModel);

Basically this is my routine: it takes the last 20 messages sorted by date, and according to origin (Client Origin == 0)

What I’d like to do is when I click "Next" he brings the next 20.

I had the initial idea to store in a variable of model (I have a MensagemModel to return the value to the View) the amount already brought from Messages, or current page to be able to have this control.

I’m a beginner so I have doubts how to pass some information between Views, as an example, Models, and how a pagination should be made.

If anyone can give me a hand, even if it is about the logic that should be followed to make this paging, sorry to ask this kind of question but I do not want this paging to get bad so I wanted a help from you, thank you.

2 answers

4


Another good option is to use one of the two components below:

Use:

// Suponha int? pagina = null como argumento da Action.
var paginaNaoNula = pagina ?? 1;
List<Mensagem> mensagens = this.mensagemServico.GetMany(l => l.LojaId == loja.LojaId && l.OrigemId != 0).OrderByDescending(l => l.DataEnvio).ToPagedList(paginaNaoNula, 20);
mensagemModel.Mensagens = mensagens;

return View(mensagemModel);
  • 1

    Cool your reply, I will check this method too, thank you very much for the attention.

  • X.PagedList.Mvc is better than Skip?

  • Yes, so much so that it has controls also for the View.

  • +1, did not know this.

2

You have to use more method o Skip

//Mensagens Enviadas
List<Mensagem> mensagens = this.mensagemServico.GetMany(l => l.LojaId == loja.LojaId && l.OrigemId != 0).OrderByDescending(l => l.DataEnvio)
.Skip(numberOfObjectsPerPage * pageNumber)
.Take(20).ToList();

mensagemModel.Mensagens = mensagens;

return View(mensagemModel);

The following code example demonstrates how to use Skip to ignore a specified number of elements in a sorted array and returns the remaining elements.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

IEnumerable<int> lowerGrades =
    grades.OrderByDescending(g => g).Skip(3);

Console.WriteLine("All grades except the top three are:");
foreach (int grade in lowerGrades)
{
    Console.WriteLine(grade);
}

/*
 This code produces the following output:

 All grades except the top three are:
 82
 70
 59
 56
*/
  • Thank you so much for your reply, I already managed to visualize how it will work, I only have one more question but it is regarding the layout , when I click on the button I wanted to change only my "messagebox" with my message, I think this question can be very comprehensive, but what I want to know and how you think it would be the best way to do this procedure, and what necessary tools. Anyway thanks for the help, it was really helpful your reply.

  • William would be nice if you’d open up another question to that

Browser other questions tagged

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