【问题标题】:How to do pagination in c# mvc如何在 c# mvc 中进行分页
【发布时间】:2013-09-18 01:44:53
【问题描述】:

如何在 C# MVC 中进行分页?

我正在尝试做这样的事情,

我想从数据库中检索 1000 条记录并将其保存在服务器端,然后我想在每个页面发送 10 条记录以在用户请求搜索页面时查看。

我已经浏览了一些示例,但我无法获得更好的解决方案,我更喜欢我建议的方式或更好的解决方案。 (我也在使用 jquery,而不是使用 Razor)

我经历过的例子

Paginated search results with LINQ to SQL

提前致谢

【问题讨论】:

标签: asp.net-mvc c#-4.0 asp.net-mvc-4 pagination


【解决方案1】:

ASP.NET MVC 中的分页是一些库解决的问题;你可以在 Nuget.org 上找到它们 http://www.nuget.org/packages?q=paging.

我想从数据库中检索 1000 条记录并将其保存在服务器端,然后我想在每个页面发送 10 条记录以在用户请求搜索页面时查看。

检索 1000 条记录并将它们保存在您的应用程序中并不理想。最好在每个请求中检索 10 条记录;正如您在问题中提到的那样,在 linq 查询中使用 SkipTake 的解决方案是完美的。但是,页面缓存数据没有问题。

【讨论】:

    【解决方案2】:

    试试这个 最简单的方法之一

    控制器

    using PagedList;
    public ActionResult Index(int ? pagePos)
    {
            //return View(db.Items.ToList());// Change this as following
            int pageNumber = (pagePos ?? 1);
            return View(db.Items.ToList().ToPagedList(pageNumber, 30)); //30 is the size of records in a single page
    }
    

    索引.cshtml

    @*@model IEnumerable<ProjectDB.Models.Item>*@
    @*Change These Also as following*@
    @model PagedList.IPagedList<ProjectDB.Models.Item>
    @using PagedList.Mvc
    
    
    <table class="table">
    <tr>
        <th>
            Name
        </th>
        <th>
            ID
        </th>
    
        <th></th>
    </tr>
    
    @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ID)
        </td>
    
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
    }   
    </table>
    
    @*Pagination Code Starts*@
    
    <div class="pageCount">
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    </div>
        @Html.PagedListPager(Model, pagePos => Url.Action("Index", new { pagePos }))
    

    【讨论】:

      猜你喜欢
      • 2010-10-01
      • 2020-04-04
      • 2010-12-17
      • 1970-01-01
      • 2016-10-11
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多