I have a user table with 10,000 entries. I want to paginate these records using LIMIT and OFFSET with PagedList but I can not do that. Currently the system searches all the records to later paginate and I do not want it to be this way, I want to paginate in a real way.
How to do this?
I'm trying like this.
Model Search
public class SearchUsuario{
public IPagedList<ViewUsuario> lista { get; set; }
public SearchUsuario(){
lista = new List<ViewUsuario>().ToPagedList(1, 50);
}
}
Model View
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 = 20;
SearchUsuario search = new SearchUsuario();
IQueryable<Usuario> lista = new UsuarioDAO().GetAll().OrderBy(u => u.nome).Skip(pageSize * pageNumber).Take(pageSize); //retorna todos os usuarios
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 HTML
@using PagedList.Mvc
@model SearchUsuario
@{
ViewBag.Title = "view";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">@Language.Users</div>
<div class="panel-body">
<div class="table-responsive">
<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>
</div><!--/div table-responsive-->
</div><!--div panel-body -->
<div class="panel-footer">
Pagina @Model.lista.PageNumber de @Model.lista.PageCount
@Html.PagedListPager(Model.lista, page => Url.Action("view", new{
page = page
}))
</div><!--/panel-footer-->
</div><!--/div panel-->
</div><!--/div col-->
</div><!--/div row-->