繁体   English   中英

按模型和计数分页webgrid

[英]Paging a webgrid by Model and Count

我一直在开发一个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的寻呼机未显示在我的视图中。 我可以设置任何财产吗?

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

autoSortAndPage: false

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

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

这应该能够以你想要的方式在页脚中进行分页。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM