简体   繁体   中英

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

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 }) )

Alternatively, this NuGet package is also very useful and easy to implement. I would highly recommend going through this utility.

https://github.com/martijnboland/mvcpaging

NuGet [PM> Install-Package MvcPaging ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM