Search code examples
c#asp.net-mvc-3pagination

The easiest way for paging with MVC3 C#?


Have a website project in MVC3 C # where I retrieve information from the database and presents in a table in my view. I want to use paging to show up to five lines per page. Have been looking for tutorials on Internet but they all seem very advanced to achieve it. What is the easiest way for paging with MVC3?

Look in the lower left corner of the picture to see what I mean by paging

paging http://www.syncfusion.com/content/en-US/products/feature/user-interface-edition/aspnet-mvc/grid/img/Paging_Larger.jpg


Solution

  • Try PagedList. There's a NuGet package for MVC.

    @{
        ViewBag.Title = "Product Listing"
    }
    @using PagedList.Mvc; //import this so we get our HTML Helper
    @using PagedList; //import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)
    
    <!-- import the included stylesheet for some (very basic) default styling -->
    <link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
    
    <!-- loop through each of your products and display it however you want. we're just printing the name here -->
    <h2>List of Products</h2>
    <ul>
        @foreach(var product in ViewBag.OnePageOfProducts){
            <li>@product.Name</li>
        }
    </ul>
    
    <!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
    @Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }) )