You can use PagedList MVC . It will help you a lot! You can do this step by step here , which will teach you how to use.
Installation, open the Package Manager Console and install through the NuGet command:
Install-Package PagedList.Mvc
In your 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 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>