按模型和计数分页webgrid

时间:2011-08-24 13:54:58

标签: c# asp.net-mvc grid pagination webgrid

我一直在开发一个asp.net mvc 3应用程序,我正在使用webgrid在我的视图中显示表格数据。我的问题是,如何通过计算数据库中有多少记录并在网格页脚中显示webgrid的寻呼机来分页数据?例如,在我的存储库中,我有:

public PagedList<Acesso> ObterAcessos(long idPessoa, int pageIndex, int pageSize)
{
    return new PagedList<Acesso>
               {
                   PageIndex = pageIndex,
                   PageSize = pageSize,
                   Total = Session.QueryOver<Acesso>().Where(acesso => acesso.Pessoa.Id == idPessoa).FutureRowCount(),
                   List = Session.QueryOver<Acesso>()
                       .Where(acesso => acesso.Pessoa.Id == idPessoa)
                       .OrderBy(acesso => acesso.DataInicio).Desc
                       .Take(pageSize).Skip((pageIndex - 1)*pageSize)
                       .Future<Acesso>()
               };
}

我的控制器:

public ActionResult Acessos(int page = 1)
{
   // 30 records per page
   return View(_rep.ObterAcessos(SecurityHelper.User.Id, page, 30));
}

我的观点:

@model PagedList<Acesso>    
@{
        ViewBag.Title = "Acessos";
        var grid = new WebGrid(rowsPerPage: Model.PageSize, canSort: false, ajaxUpdateContainerId: "grid");
        // set the rowCount by Total property and the List property
        grid.Bind(rowCount: Model.Total, source: Model.List);
    }

        <h2>Acessos</h2>

        @grid.GetHtml(htmlAttributes: new { id="grid", style="width:700px;" },
            mode: WebGridPagerModes.All,
            tableStyle: "grid", rowStyle: "gridrow", alternatingRowStyle: "gridrow_alternate",
            columns: grid.Columns(
                    grid.Column("DataInicio", "Data de Inicio", item => item.DataInicio.ToString("dd/MM/yyyy HH:mm")), 
                    grid.Column("IP", "IP"))
            )

数据正确,计数和列表是正确的,但是当我运行此代码时,它不会页面数据,它只显示第一页,而webgrid的寻呼机未显示在我的视图中。我可以设置任何财产吗?

1 个答案:

答案 0 :(得分:5)

由于您在模型中进行分页,因此应添加:

autoSortAndPage: false

到你的grid.Bind()所以它读取:

grid.Bind(rowCount: Model.Total, source: Model.List, autoSortAndPage: false);

这应该按照你想要的方式启用页脚中的分页。