0
I have a user table with 10,000 entries. I want to paginate these records using LIMIT and OFFSET with Pagedlist but I’m not able to do that. Currently the system searches all records to then paginate and do not want it to be like this, 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-->
What is the database? on this line Iqueryable<Usuario> list = new User(). Getall(); should return only the amount of record and the amount of records returned from a given
page
? that is, what is insideGetAll()
?– novic
@Virgilionovic on this line
GetAll()
returns all records. And the eh Mysql database.– FernandoPaiva
then the code of this method is wrong? has how to put the question?
– novic
@Virgilionovic ready. I put the
GetAll()
that you’re in classGenericDAO
and returns all records– FernandoPaiva
oh it is not difficult to solve, without having to put the model of the class Viewusuario?
– novic
@Virgilionovic posted the class
ViewUsuario
and the change I made to Controller. Controller paid, butView HTML
does not show that you have other pages– FernandoPaiva
The answer came true?
– novic