English 中文(简体)
How to do paging in the mvc grid?
原标题:
  • 时间:2009-11-18 12:08:09
  •  标签:
  • paging

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

问题回答

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!





相关问题
paging next and previous links don t work!

can anyone help me with this code?the next and previous links don t work $limit=20; mysql_connect("localhost","root",""); mysql_select_db("autoschool") or die("Unable to select database"); $query = "...

Please help me with ASP.NET MVC Paging error

I am getting the following error in my mvc application when I am doing the paging functionality CS1061: System.Collections.Generic.IEnumerable does not contain a definition for HasPreviousPage ...

How to do paging in the mvc grid?

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

What s the best method to do paging in my ASP page

What s the best method to do paging in my ASP page when displaying a list of items? I knew that there is no equivalent to MySQL s LIMIT clause present in SQL Server, either 2000 or 2005. How can I ...

linq paging - get total rows

i have a question about linq. I m using Skip and Take to do paging: (from l in db.ProductList select l).Skip((page - 1) * row_per_page).Take(row_per_page) It work, and i need retrieve ...

Java, Page through array

Test[] array = new Test[3]; array[0] = new RowBoat("Wood", "Oars", 10); array[1] = new PowerBoat("Fiberglass", "Outboard", 35); array[2] = new SailBoat("Composite", "Sail", 40); I have ...

Mod Rewrite Optional Parameters

I have a fairly complex set of rewrite rules to give my site pages pretty URLs. Right now to deal with paging of search results I m using 2 rewrite rules: RewriteRule ^search/([0-9]+)$ /cgi-bin/...

热门标签