在MVC4中使用WebGrid分页

时间:2013-09-05 08:29:48

标签: asp.net-mvc asp.net-mvc-4 pagination webgrid

这是我视图中的代码。 WebGrid排序和过滤器工作正常。我正在尝试实现没有发生的分页。

查看:

    @{        
    var grid = new WebGrid(null,
            defaultSort: "",
            rowsPerPage: 10,
            selectionFieldName:"SearchString",
            ajaxUpdateContainerId:"agrid",
            sortFieldName:"SortColumn",
            sortDirectionFieldName:"SortOrder");
    grid.Bind(Model, autoSortAndPage: true, rowCount:20);
}

<div id="agrid">
@grid.GetHtml(
    tableStyle: "webGrid",
    headerStyle: "header",
    alternatingRowStyle: "alt",
    columns: grid.Columns(
        grid.Column("ID"),
        grid.Column("Description")
    )
)
 </div> 

控制器:

        public ActionResult Index(int? page=1, int currentPage = 1, int PageSize = 10, string SortColumn = "", string SortOrder = "", string SearchString = "", int numberOfPages=0, int tr=0)
    {
        DataContextDataContext obj = new DataContextDataContext();
        System.Nullable<int> Total = null;


        var model = obj.TempItemSubClassList(page, PageSize, SortColumn, SortOrder, SearchString, ref Total).ToList();
        tr = (int) Total;

        numberOfPages = (int)(Total + PageSize - 1) / PageSize;

        return View(model);

        //var itemsubclasses = db.ItemSubClasses.Include(i => i.ItemClass);
        //return View(itemsubclasses.ToList());

    }

1 个答案:

答案 0 :(得分:2)

网格需要知道您提供的示例中的行总数已经硬编码为20.将总数添加到模型中并以此方式引用它。此外,有点违反直觉,autoSortAndPage参数应设置为false

@{        
var grid = new WebGrid(null,
        defaultSort: "",
        rowsPerPage: 10,
        selectionFieldName:"SearchString",
        ajaxUpdateContainerId:"agrid",
        sortFieldName:"SortColumn",
        sortDirectionFieldName:"SortOrder");
grid.Bind(Model, autoSortAndPage: true, rowCount:20); <--- total number of rows
}