zoukankan      html  css  js  c++  java
  • PagedList.MVC分页

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace PagedList.MVCWeb.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index(int pageIndex=1)
            {
                int vPageCount = 0;
                List<Product> vProductList = GetPageSource(pageIndex, out vPageCount);
                StaticPagedList<Product> vStaticPagedList = new StaticPagedList<Product>(vProductList, pageIndex, 3, vPageCount);
                return View(vStaticPagedList);
            }
    
            // 可改成读数据库
            [NonAction]
            List<Product> GetPageSource(int argPageindex,out int argPageIndex)
            {
                int vPageSize=3;
                List<Product> vProductList = DataSource();
                argPageIndex = vProductList.Count();
                return vProductList.Skip((argPageindex - 1) * vPageSize).Take(vPageSize).ToList();
            }
    
            [NonAction]
            List<Product> DataSource()
            {
                return new List<Product>() { 
                        new Product() { 
                            ID=1,
                            Name="Name1",
                            Url="URL1",
                            Price=1,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=2,
                            Name="Name2",
                            Url="URL2",
                            Price=2,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=3,
                            Name="Name3",
                            Url="URL3",
                            Price=3,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=4,
                            Name="Name4",
                            Url="URL4",
                            Price=4,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=5,
                            Name="Name5",
                            Url="URL5",
                            Price=5,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=6,
                            Name="Name6",
                            Url="URL6",
                            Price=6,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=7,
                            Name="Name7",
                            Url="URL7",
                            Price=7,
                            CreateDate=DateTime.Now
                        } ,
                        new Product() { 
                            ID=8,
                            Name="Name8",
                            Url="URL8",
                            Price=8,
                            CreateDate=DateTime.Now
                        } 
                };
            }
        }
    }
    

    Index View

    @using PagedList.Mvc
    @using PagedList.MVCWeb
    @model PagedList.StaticPagedList<Product>
     
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <link href="~/Content/PagedList.css" rel="stylesheet" />
        <title>Index</title>
    </head>
    <body>
        @using(Html.BeginForm())
        {
            <div>
                <table>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>URL</th>
                        <th>Price</th>
                        <th>CreteDate</th>
                    </tr>
                       @foreach (Product item in Model)
                       {
                            <tr>
                                   <td>@item.ID</td>
                                   <td>@item.Name</td>
                                   <td>@item.Url</td>
                                   <td>@item.Price</td>
                                   <td>@item.CreateDate</td>
                            </tr>
                       }
                    </table>
    
                @Html.PagedListPager((PagedList.IPagedList)Model, x => Url.Action("Index", new {pageIndex=x}))
            </div>
        }
    </body>
    </html>

    装了PagedList.Mvc,”~/Content/PagedList.css“NuGet会自动的放在你的Content中

  • 相关阅读:
    最大回文子串
    求数组中的最大连续子序列和
    如何在10亿数中找出前1000大的数
    给定一个字符串,最多删除一个字符,判断能够构成回文字符串
    HashMap(数组+链表+红黑树)、HashTable、TreeMap
    【转】jmeter如何设置登录接口只调用一次以及遇到的问题:cookie参数放在消息头headers里面
    Kafka命令行操作
    git上无法push代码解决办法
    【转】Jenkins集成Docker镜像实现自动发布
    awk命令总结
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/3777432.html
Copyright © 2011-2022 走看看