MVC 3 + WebGrid:分页控件不显示

发布于 2024-12-03 15:54:04 字数 2220 浏览 0 评论 0原文

我在对 WebGrid 进行分页时遇到问题。本质上,我传递了完整的数据,它分页正常。如果我将数据一次限制为一页,则分页控件就会消失,尽管我已经传入了 rowCount。

为什么?

请参阅下面的设置:

我有一个网格定义如下:

@{
    ViewBag.Title = "Cuentas";
    var grid = new WebGrid(defaultSort: "AccountName", rowsPerPage: 30, canPage: true, 
        ajaxUpdateCallback: "updateGrid");
    grid.Bind(source: Model.Accounts, rowCount: Model.TotalRows);
}

其列定义如下:

<div id="grid">
    @grid.GetHtml(tableStyle:"grid",headerStyle:"head", alternatingRowStyle:"alt", htmlAttributes: new { id = "AccountsGrid"},
        columns:  grid.Columns(grid.Column(header: "", format: (item) => Html.ActionLink("Editar", "Edit", new { id = item.AccountId })),
                                grid.Column(header: "", format: (item) => Html.ActionLink("Eliminar", "Delete", new { id = item.AccountId })),
                                grid.Column("AccountName",header:"Nombre"), 
                                grid.Column("IsClient", header:"Cliente?"), 
                                grid.Column("IsProvider", header:"Proveedor?"), 
                                grid.Column("IsBank", header:"Banco o Caja?"),
                                        grid.Column("Person", format: item => new HtmlString(item.Person == null ? "" : item.Person.Name), header: "Contacto"),
                                        grid.Column("AccountsCostCentress", 
                                            format: item => new HtmlString(item.AccountsCostCentress == null ?
                                            "" : item.AccountsCostCentress.Count.ToString()), header: "Actividades")))
</div>

我在视图中使用的 ViewModel 定义如下:

public class AccountsListViewModel
{
    public int PageSize { get; set; }
    public int PageNumber { get; set; }
    public IEnumerable<Account> Accounts { get; set; }
    public int TotalRows {get;set;}
}

这表明我可以传递 rowCount 和 IEnumerable Accounts。

假设我有 305 行帐户。如果我将PageSize设置为30,这意味着我将有11页数据(最后一页只有5条记录)。如果帐户保存所有 305 条记录,则网格将与分页一起工作。

如果Accounts仅保存第3页对应的30条记录,则分页控件不会显示,即使TotalRows == 305。

无法弄清楚。你可以吗?或者你知道我可能会看的博客文章、主题或文章吗?

I am having problems with paging a WebGrid. Essentially, I pass the full data, it pages OK. If I limit the data to one page at a time, the paging controls disappear, despite the fact that I have passed in the rowCount.

Why?

See below for setup:

I have a grid defined as follows:

@{
    ViewBag.Title = "Cuentas";
    var grid = new WebGrid(defaultSort: "AccountName", rowsPerPage: 30, canPage: true, 
        ajaxUpdateCallback: "updateGrid");
    grid.Bind(source: Model.Accounts, rowCount: Model.TotalRows);
}

With its columns defined as follows:

<div id="grid">
    @grid.GetHtml(tableStyle:"grid",headerStyle:"head", alternatingRowStyle:"alt", htmlAttributes: new { id = "AccountsGrid"},
        columns:  grid.Columns(grid.Column(header: "", format: (item) => Html.ActionLink("Editar", "Edit", new { id = item.AccountId })),
                                grid.Column(header: "", format: (item) => Html.ActionLink("Eliminar", "Delete", new { id = item.AccountId })),
                                grid.Column("AccountName",header:"Nombre"), 
                                grid.Column("IsClient", header:"Cliente?"), 
                                grid.Column("IsProvider", header:"Proveedor?"), 
                                grid.Column("IsBank", header:"Banco o Caja?"),
                                        grid.Column("Person", format: item => new HtmlString(item.Person == null ? "" : item.Person.Name), header: "Contacto"),
                                        grid.Column("AccountsCostCentress", 
                                            format: item => new HtmlString(item.AccountsCostCentress == null ?
                                            "" : item.AccountsCostCentress.Count.ToString()), header: "Actividades")))
</div>

The ViewModel I am using in my view is defined as follows:

public class AccountsListViewModel
{
    public int PageSize { get; set; }
    public int PageNumber { get; set; }
    public IEnumerable<Account> Accounts { get; set; }
    public int TotalRows {get;set;}
}

This shows that I can pass the rowCount and the IEnumerable Accounts.

Lets say I have 305 rows of accounts. If I set PageSize to 30, that means I will have 11 pages of data (the last one will only have 5 records on it). If Accounts holds all 305 records, the grid works with paging and all.

If Accounts only holds the 30 records corresponding to page 3, for example, the paging control don't show, EVEN IF TotalRows == 305.

Can't figure it out. Can you? Or do you know of a blog post, thread or article I might look at??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知足的幸福 2024-12-10 15:54:04

对不起!回答了我自己的问题:

autoSortPage: grid.Bind(...) 方法中缺少 false 参数。

请参阅以下 SO 线程:

WebGrid 分页

Sorry! Answered my own question:

autoSortPage: false parameter missing from grid.Bind(...) method.

See the following SO thread:

WebGrid paging

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文