You can use the Pagedlist MVC. It will help you a lot! You can do this step by step here, that will teach you to use.
Install, open the Package Manager Console and install via the Nuget command:
Install-Package PagedList.Mvc
In his Controller, you define how many records will appear per page:
public ActionResult Index(int? pagina)
{
var contexto = new CadastroEntities();
var listaAlunos = contexto.Alunos.ToList();
int paginaTamanho = 4;
int paginaNumero = (pagina ?? 1);
return View(listaAlunos.ToPagedList(paginaNumero, paginaTamanho));
}
and in the View:
<div>
Página @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
de @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { pagina = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Anterior", "Index", new { pagina = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Anterior
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Próxima >", "Index", new { pagina = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { pagina = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:Próxima >
@Html.Raw(" ")
@:>>
}
</div>
It worked, thank you very much.
– Daniel Gregatto