zoukankan      html  css  js  c++  java
  • MvcPager分页控件的使用

    1.引入MvcPager.dll(MvcPager分页控件:http://www.webdiyer.com/mvcpager/)

    2.后台C# Controller:

    //Ddemo使用Webdiyer.MvcPager的分页方法

            /// <summary>

            /// 分页显示

            /// 使用Webdiyer.MvcPager的分页方法Demo

            /// </summary>

            /// <param name="pageIndex">页码,第几页(从第一页开始),与前端声明的参数对应</param>

            public ActionResult Index(int pageIndex = 1 )

            {

                //使用Webdiyer.MvcPager的ToPagedList()方法

                //返回自定义的PagedList<Test>类型,而非.NET标准的List或Queryable类型

                //会查两次数据库:1.总记录数。2.当页数量的记录

                PagedList<Test> wholeData = db.Tests.OrderBy(t => t.Sno).ToPagedList(pageIndex, 5);   

                //视图的模型类型会不匹配而出错。即使修改视图头部的model声明也不行,因为使用了Webdiyer.MvcPager的Pager分页方法

                //var wholeData = db.Tests.OrderBy(t => t.Sno).Skip((pageIndex - 1) * 5).Take(5);  

                return View(wholeData);

            }

    3.前端html:

             @Html.Pager(Model, new PagerOptions

                        {   PageIndexParameterName = "pageIndex",     //对应控制器参数

                            ShowPageIndexBox = true,

                            PageIndexBoxType = PageIndexBoxType.DropDownList,

                            ShowGoButton = false,

                            NumericPagerItemCount = 4

                        }

                    )

    4.详见TestControllerIndex()方法。

     1  public class TestController : Controller
     2     {
     3         TestContext db = new TestContext();
     4 
     5         //Ddemo使用Webdiyer.MvcPager的分页方法
     6         /// <summary>
     7         /// 分页显示
     8         /// 使用Webdiyer.MvcPager的分页方法Demo
     9         /// </summary>
    10         /// <param name="pageIndex">页码,第几页(从第一页开始)</param>
    11         public ActionResult Index(int pageIndex = 1 )
    12         {
    13             //使用Webdiyer.MvcPager的ToPagedList()方法
    14             //返回自定义的PagedList<Test>类型,而非.NET标准的List或Queryable类型
    15             //会查两次数据库:1.总记录数。2.当页数量的记录
    16             PagedList<Test> wholeData = db.Tests.OrderBy(t => t.Sno).ToPagedList(pageIndex, 5);    
    17 
    18             //视图的模型类型会不匹配而出错。即使修改视图头部的model声明也不行,因为使用了Webdiyer.MvcPager的Pager分页方法
    19             //var wholeData = db.Tests.OrderBy(t => t.Sno).Skip((pageIndex - 1) * 5).Take(5);   
    20             return View(wholeData);
    21         }
    22 }
    View Code
     1 @using System.Collections.Generic;
     2 @using Webdiyer.WebControls.Mvc;
     3 @*@model IQueryable<TestModel.Models.Test>*@
     4 @model PagedList<TestModel.Models.Test>
     5 @{
     6     ViewBag.Title = "学生列表";
     7 }
     8 <h2>@ViewBag.Title</h2>
     9 @Html.ActionLink("增加", "Create")
    10 @if (Model != null)
    11 { 
    12     <table>
    13         <tr>
    14             <td>
    15                 学号
    16             </td>
    17             <td>
    18                 姓名
    19             </td>
    20             <td>
    21                 年龄
    22             </td>
    23             <td>
    24                 操作
    25             </td>
    26             <td>
    27                 所选课程查询
    28             </td>
    29         </tr>
    30         @foreach (var dt in Model)
    31         {
    32             <tr>
    33                 <td>@dt.Sno
    34                 </td>
    35                 <td>@dt.Sname
    36                 </td>
    37                 <td>@dt.Age
    38                 </td>
    39                 <td>
    40                     @Html.ActionLink("编辑", "Edit", new { sNo = dt.Sno }, null)
    41                     @Html.ActionLink("删除", "Delete", new { sNo = dt.Sno }, new { onclick = "javascript:return confirm('确认要删除吗?');" })
    42                 </td>
    43                 <td>@Html.ActionLink("查询", "Index", "SelectCourse", new { sNo = dt.Sno }, null)
    44                 </td>
    45             </tr>
    46         
    47         }
    48     </table>
    49     @Html.Pager(Model, new PagerOptions
    50                     {   PageIndexParameterName = "pageIndex",
    51                         ShowPageIndexBox = true,
    52                         PageIndexBoxType = PageIndexBoxType.DropDownList,
    53                         ShowGoButton = false,
    54                         NumericPagerItemCount = 4
    55                     }
    56                 )
    57 }
    View Code
  • 相关阅读:
    第02组 Beta冲刺(4/4)
    第02组 Beta冲刺(2/4)
    第02组 Beta冲刺(3/4)
    第02组 Beta冲刺(1/4)
    第02组 Alpha事后诸葛亮
    第02组 Alpha冲刺(4/4)
    第02组 Alpha冲刺(3/4)
    第02组 Alpha冲刺(2/4)
    第02组 Alpha冲刺(1/4)
    第02组 Beta版本演示
  • 原文地址:https://www.cnblogs.com/nlh774/p/3623312.html
Copyright © 2011-2022 走看看