The easiest way for paging with MVC3 C#?

与世无争的帅哥 提交于 2019-12-21 05:12:16

问题


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


回答1:


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



回答2:


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 ]



来源:https://stackoverflow.com/questions/8070030/the-easiest-way-for-paging-with-mvc3-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!