如何在mvc网格中进行分页?

发布于 2024-08-11 11:46:41 字数 50 浏览 5 评论 0原文

关于如何以最简单的方式在 mvc 网格中进行分页有什么建议吗?请指出示例代码(如果有)

Any suggestions on how to do paging in the mvc grid, in an easiest possible way? Please point me to sample code, if any

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

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

发布评论

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

评论(1

陈甜 2024-08-18 11:46:41

里兹,

我认为这个问题已经被问过,但这是我指的问题: 分页&使用 ASP.Net MVC 对网格进行排序

另外,这是一个关于如何执行此操作的非常简洁的教程:
http://xlib.wordpress.com/2009/06/29/asp-net-mvc- grid-%E2%80%93-part-2-paging/

上述链接中的一些片段:

使用它在页面上获取选择器,以允许用户确定每页显示的行数:

<%= Html.DropDownList("pageSize", CustomerController.PageSizeSelectList(), new { onchange = "onPageSizeChange()" })%> rows per page

然后在自定义页面控制器中编写以下代码:

public static SelectList PageSizeSelectList()
{
    var pageSizes = new List {"1", "2", "5", "10", "100"};
    return new SelectList(pageSizes, "Value");
}

现在将 JavaScript 添加到页面:

//Set hidden variable to go to next/prev/last/first page and submit the form
function goToPage(pageIndex) {
    $("#currentPage").val(pageIndex);
    $("#gridAction").val("CurrentPageChanged");

    submitForm();
}

//Set action performed in hidden variable. When PageSize changes - PageIndex needs to be
//reset to 1. This logic will go on the server side.
function onPageSizeChange(pageIndex) {
    $("#gridAction").val("PageSizeChanged");
    submitForm();
}

function submitForm() {
    var form = $("#grid").parents("form:first");
    form.submit();
}

然后更新页面控制器以执行分页:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(int currentPage, int pageSize, string gridAction)
{
        //Do logic depending on what action was performed
    if (gridAction == "PageSizeChanged")
        currentPage = 1;

        //Check if there are no results.  In this case return empty list.
    IQueryable query = _customerService.GetQueryable();
    int totalRows = query.Count();
    if (totalRows==0)
        return View(new List());

    int totalPages = (int)Math.Ceiling((double)totalRows / (double)pageSize);
    if (totalPages != 1)
    {
        //use LINQ to perform paging
        query = query.Skip((currentPage - 1) * pageSize)
                        .Take(pageSize);
    }

        //Update ViewData collection to display pager stats and pager controls
    UpdatePagerViewData(totalPages, totalRows, currentPage, pageSize);

    List customers = query.ToList();
    return View(customers);
}

我希望这有帮助,

谢谢!

Ritz,

I think this question has been asked already, but here is the question I'm referring to: Paging & Sorting grids with ASP.Net MVC

Also, this is a very concise tutorial on how to do this:
http://xlib.wordpress.com/2009/06/29/asp-net-mvc-grid-%E2%80%93-part-2-paging/

Some snippets from the above link:

Use this to get a selector on your page to allow the user to determine the number of rows to display per page:

<%= Html.DropDownList("pageSize", CustomerController.PageSizeSelectList(), new { onchange = "onPageSizeChange()" })%> rows per page

Then in a custom page controller write this code:

public static SelectList PageSizeSelectList()
{
    var pageSizes = new List {"1", "2", "5", "10", "100"};
    return new SelectList(pageSizes, "Value");
}

Now add the JavaScript to the page:

//Set hidden variable to go to next/prev/last/first page and submit the form
function goToPage(pageIndex) {
    $("#currentPage").val(pageIndex);
    $("#gridAction").val("CurrentPageChanged");

    submitForm();
}

//Set action performed in hidden variable. When PageSize changes - PageIndex needs to be
//reset to 1. This logic will go on the server side.
function onPageSizeChange(pageIndex) {
    $("#gridAction").val("PageSizeChanged");
    submitForm();
}

function submitForm() {
    var form = $("#grid").parents("form:first");
    form.submit();
}

Then update your page controller to perform the paging:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(int currentPage, int pageSize, string gridAction)
{
        //Do logic depending on what action was performed
    if (gridAction == "PageSizeChanged")
        currentPage = 1;

        //Check if there are no results.  In this case return empty list.
    IQueryable query = _customerService.GetQueryable();
    int totalRows = query.Count();
    if (totalRows==0)
        return View(new List());

    int totalPages = (int)Math.Ceiling((double)totalRows / (double)pageSize);
    if (totalPages != 1)
    {
        //use LINQ to perform paging
        query = query.Skip((currentPage - 1) * pageSize)
                        .Take(pageSize);
    }

        //Update ViewData collection to display pager stats and pager controls
    UpdatePagerViewData(totalPages, totalRows, currentPage, pageSize);

    List customers = query.ToList();
    return View(customers);
}

I hope this helps,

Thanks!

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