zoukankan      html  css  js  c++  java
  • ASP.NET MVC 4使用PagedList.Mvc分页

      ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList、PagedList.Mvc进行分页。

      1. 通过NuGet引用PagedList.Mvc

      在安装引用PagedList.Mvc的同时会安装引用PagedList。

      2. PagedList.Mvc基本使用

      ProvinceController.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    using MvcExample.Models;
    
    using PagedList;
    
    namespace MvcExample.Controllers
    {
        public class ProvinceController : Controller
        {
            private MvcExampleContext ctx = new MvcExampleContext();
    
            public ActionResult Index(int page = 1)
            {
                return View(ctx.Provinces.OrderBy(p => p.ProvinceID).ToPagedList(page, 10));
            }
    
            protected override void Dispose(bool disposing)
            {
                ctx.Dispose();
                base.Dispose(disposing);
            }
        }
    }

      Index.cshtml:

    @model PagedList.IPagedList<MvcExample.Models.Province>
    @using PagedList.Mvc;
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <style type="text/css">
            ul.pagination
            {
                margin: 0;
            }
            .pagination li
            {
                list-style-type: none;
                float: left;
                min-width: 15px;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th>
                    省份ID
                </th>
                <th>
                    省份编号
                </th>
                <th>
                    省份名称
                </th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProvinceID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProvinceNo)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProvinceName)
                    </td>
                </tr>
            }
        </table>
        @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
    </body>
    </html>

      运行效果:

      引入PagedList.Mvc分页之后生成的分页Html代码:

       分页执行的SQL语句:

    SELECT TOP (10) 
        [Extent1].[ProvinceID] AS [ProvinceID], 
        [Extent1].[ProvinceNo] AS [ProvinceNo], 
        [Extent1].[ProvinceName] AS [ProvinceName]
        FROM ( SELECT [Extent1].[ProvinceID] AS [ProvinceID], [Extent1].[ProvinceNo] AS [ProvinceNo], [Extent1].[ProvinceName] AS [ProvinceName], row_number() OVER (ORDER BY [Extent1].[ProvinceID] ASC) AS [row_number]
            FROM [dbo].[Province] AS [Extent1]
        )  AS [Extent1]
        WHERE [Extent1].[row_number] > 10
        ORDER BY [Extent1].[ProvinceID] ASC

      参考资料:

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

  • 相关阅读:
    WEB引入Google思源黑体
    Linux安装最新版Node.js
    JS判断值是否是数字
    高效jQuery的奥秘
    一个Web前端工程师或程序员的发展方向,未来困境及穷途末路
    javascript 模块化 (切记:学习思想)
    学习 正则表达式 js java c# python 通用
    Promise如何解决回调地狱
    VSCode 开发插件 推荐
    js移动端自适应动态设置html的fontsize
  • 原文地址:https://www.cnblogs.com/libingql/p/3486554.html
Copyright © 2011-2022 走看看