我正在使用PagedList
进行分页,但是它需要将所有记录放在列表中然后进行分页,这将浪费很长时间来呈现页面,因此,我想使用LIMIT和OFFSET使用PagedLit进行真正的分页并浪费很短的时间。
我如何使用带有LIMIT和OFFSET的PagedList来创建真正的分页?
试图
模型
public class SearchUsuarioModel{
public IPagedList<UpUsuarioAdminModel> listaModel { get; set; } //list model
public IPagedList<Usuario> listaObject { get; set; } //list all objects
//Status
public IEnumerable<SelectListItem> status { get; set; }
public int statusSelected { get; set; }
public String nome { get; set; }
public String role { get; set; }
public int limit { get; set; }
public int offset { get; set; }
public int pageSize { get; set; }
public int pageNumber { get; set; }
public SearchUsuarioModel(){
listaModel = new List<UpUsuarioAdminModel>().ToPagedList(1, 50);
}
}
控制器
public ActionResult viewAllAdmin(int? page, String nome, String role, int? offSet){
//model
SearchUsuarioModel model = new SearchUsuarioModel();
model.nome = nome;
model.role = role;
model.limit = 3;
model.offset = offSet ?? 0;
model.pageNumber = page ?? 1;
model.pageSize = model.limit;
//listas
IList<Usuario> _listaObject = new List<Usuario>();
IList<UpUsuarioAdminModel> _listaModel = new List<UpUsuarioAdminModel>();
UsuarioDAO dao = new UsuarioDAO();
try{
_listaObject = dao.findAllOffset(model.limit, model.offset);
} catch (Exception e){
Debug.WriteLine("viewAllAdmin UsuarioController: " + e.Message);
}
if (_listaObject.Count > 0){
foreach (Usuario u in _listaObject){
UpUsuarioAdminModel m = new UpUsuarioAdminModel();
m.id = u.id;
m.nome = u.nome;
m.email = u.email;
m.roleSelected = u.role.ToString();
m.statusDesc = u.status == 1 ? "Ativo" : "Inativo";
m.contrato = u.imgContrato;
if (u.contato != null){
//Debug.WriteLine("Telefone: " + string.IsNullOrEmpty(u.contato.telefone1));
m.telefone1 = string.IsNullOrEmpty(u.contato.telefone1) ? "" : u.contato.telefone1;
}
_listaModel.Add(m);
}
}
model.listaModel = _listaModel.ToPagedList(model.pageNumber, model.limit);
model.listaObject = dao.findAll().ToPagedList(model.pageNumber, model.limit);
return View(model);
}
HTML
<div class="panel panel-red center-block">
<div class="panel-heading bg-red clearfix">
<strong>Usuários do sistema</strong>
<div class="pull-right">
@Html.ActionLink("Novo", "addUsuarioAdmin", "Usuario", new { Class = "text-white glyphicon glyphicon-plus" })
</div>
</div>
<div class="panel-body">
<table class="table table-bordered table-responsive table-striped table-hover" id="tableView">
<thead class="CabecalhoTabela">
<tr>
<th>#ID</th>
<th>Nome</th>
<th>Email</th>
<th>Telefone</th>
<th>Perfil</th>
<th>Status</th>
<th>Controles</th>
</tr>
</thead>
<tbody class="conteudoTabela">
@foreach (UpUsuarioAdminModel m in Model.listaModel){
<tr>
<td class="text-right">@Html.DisplayFor(i => m.id)</td>
<td>@Html.DisplayFor(i => m.nome)</td>
<td class="text-lowercase">@Html.DisplayFor(i => m.email)</td>
<td class="text-center">@Html.DisplayFor(i => m.telefone1)</td>
<td class="text-center">@Html.DisplayFor(i => m.roleSelected)</td>
<td class="text-center">@Html.DisplayFor(i => m.statusDesc)</td>
<td>
@Html.ActionLink(" ", "editUsuarioAdmin", "Usuario", new { id = EncodingParams.encode(Convert.ToString(@m.id)) }, new { Class = "glyphicon glyphicon-pencil", title = "editar" })
@if (!string.IsNullOrEmpty(m.contrato))
{
@Html.ActionLink(" ", "Download", "Usuario", new { id = EncodingParams.encode(Convert.ToString(@m.id)) }, new { Class = "glyphicon glyphicon-file", title = "Contrato" })
}
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="panel-footer">
Pagina @Model.listaObject.PageNumber de @Model.listaObject.PageCount
@Html.PagedListPager(Model.listaObject, page => Url.Action("viewAllAdmin", new { page = Model.pageNumber, offSet = Model.offset }))
</div>
</div>
DAO
public IList<Usuario> findAll(){
ISession _session = getSession();
IList<Usuario> list = _session.CreateQuery("FROM Usuario u ORDER BY u.id")
.List<Usuario>();
return list;
}
public IList<Usuario> findAllOffset(int limit, int offset){
ISession _session = getSession();
IList<Usuario> list = _session.CreateQuery("FROM Usuario u ORDER BY u.id")
.SetFirstResult(offset)
.SetMaxResults(limit)
.List<Usuario>();
return list;
}