【问题标题】:ASP.Net MVC 3.0 Razor View Grid Ajax PaginationASP.Net MVC 3.0 Razor 视图网格 Ajax 分页
【发布时间】:2011-12-03 02:30:09
【问题描述】:

如何为我拥有的这个 Grid 实现 Ajax?

@{
var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 5);
}
@if (Model.Count() > 0)
{
<div id="grid">
    @grid.GetHtml(
 tableStyle: "grid",
 headerStyle: "head",
 alternatingRowStyle: "alt",
 columns: grid.Columns(
 grid.Column("FirstName", "First Name"),
 grid.Column("LastName", "Last Name"),
 grid.Column("Address"),
 grid.Column("DOB"),
 grid.Column("Gender")
 )
    )
</div>
}

我尝试添加 ajaxUpdateContainerId,但它给出了一些错误。 没看懂。

我已经创建了如下所示的网格

var grid = new WebGrid(canPage: true, rowsPerPage: 2, defaultSort: "FirstName", canSort: true, ajaxUpdateContainerId: "grid");
grid.Bind(Model, rowCount:Model.Count(), autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);

但它显示了网格中的所有记录,我只想在网格中显示 5 条记录然后分页

【问题讨论】:

    标签: ajax asp.net-mvc pagination


    【解决方案1】:

    这里有一篇很棒的文章http://www.unboxedsolutions.com/sean/archive/2011/01/23/15964.aspx

    @using AdventureWorks.Common
    @using ThisController = MvcDemo.Controllers.GridExampleController
    @model GridExampleViewModel
    
    @{ ViewBag.Title = "Index"; }
    
    @{
        var grid = new WebGrid(canPage: true, rowsPerPage: ThisController.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
        grid.Bind(Model.Employees, rowCount: Model.TotalRecords, autoSortAndPage: false);
        grid.Pager(WebGridPagerModes.All);
        @grid.GetHtml(htmlAttributes: new { id="grid" },
            columns: grid.Columns(
                grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { EmployeeID = item.EmployeeID, ContactID = item.ContactID })),
                grid.Column("FullName"),
                grid.Column("Title")
            ));
    }
    

    【讨论】:

    • 我已经创建了这样的网格 var grid = new WebGrid(canPage: true, rowsPerPage: 2, defaultSort: "FirstName", canSort: true, ajaxUpdateContainerId: "grid"); grid.Bind(Model, rowCount:Model.Count(), autoSortAndPage: false); grid.Pager(WebGridPagerModes.All);
    • 您的模型中似乎没有使用 skip 和 take 方法。 queryResults.Skip(page * records).Take(records)
    • 我做了类似这样的事情 grid.Bind(Model.Take(5), rowCount: Model.Count(), autoSortAndPage: false);它列出了 50 条记录中的 5 条记录,当我单击第二页时,它仍将仅显示前 5 条记录
    • 是的,它将从您的表中获取 5 条记录,请按照我提供的示例页面进行操作,您需要将分页添加到您的模型中。
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多