I have one table on database with 10.000 records and I wanna paginate it using PagedList. The problem is that all records are loaded and after that the PagedList paginate them, it is become very slow. I want to make a real paginate for example returning each 20 records till 10.000 and showing them on view, but I cannot make it.
How could I do this ? Any example ?
Model Search
public class SearchUsuario
{
public IPagedList<ViewUsuario> lista { get; set; }
public SearchUsuario(){
lista = new List<ViewUsuario>().ToPagedList(1, 50);
}
}
Model ViewUsuario
public class ViewUsuario
{
public String nome { get; set; }
}
Generic DAO
//returns all records
public IQueryable<T> GetAll()
{
IQueryable<T> query = context.Set<T>();
return query;
}
Controller
public ActionResult view(int? page)
{
int pageNumber = page ?? 1;
int pageSize = 10;
SearchUsuario search = new SearchUsuario();
IQueryable<Usuario> lista = new UsuarioDAO().GetAll(); //returns all records
List<ViewUsuario> listaModel = new List<ViewUsuario>();
foreach(Usuario u in lista){
Debug.WriteLine(u.nome);
ViewUsuario view = new ViewUsuario();
view.nome = u.nome;
listaModel.Add(view);
}
search.lista = listaModel.ToPagedList(pageNumber, pageSize);
return View(search);
}
View
@using PagedList.Mvc
@model SearchUsuario
....
<table id="grid" class="table table-striped table-hover" cellspacing="0">
<thead>
<tr>
<th>Nome</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Nome</th>
</tr>
</tfoot>
<tbody>
@foreach (ViewUsuario m in Model.lista){
<tr>
<td>@Html.DisplayFor(i => m.nome)</td>
</tr>
}
</tbody>
</table>
....
Pagina @Model.lista.PageNumber de @Model.lista.PageCount
@Html.PagedListPager(Model.lista, page => Url.Action("view", new{ page = page }))