1.新建MVC项目

2.在Web项目下添加PagedList

nuget 命令 :install-package pagedlist.mvc

在安装完成之后,会在Content下添加PagedList.css文件,这个是在分页中会用到的样式

3.有些人用的数据来源是其他地方来的,就用Ilist 的类型进行分页吧

顺便做一下json文件的转换

安装newtonsoft.json

Nuget 命令 :install-package newtonsoft.json

4.在Models中添加model类型定义 TestModel ,用来转换的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MvcApplication2.Models
{
public class TestModel
{
public string Name { get; set; } public int? Age { get; set; } public string Remark { get; set; }
}
}

5.创建Controller 进行管理,代码中使用局部页添加到index页中,分页之后还是跳转到局部页中,就是随笔一记,不做多的处理

index ACtion中产生数据,并将其计入到静态变量中国

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
using Newtonsoft.Json;
using PagedList; namespace MvcApplication2.Controllers
{
public class PartTestController : Controller
{
//
// GET: /PartTest/
static IList<TestModel> models = new List<TestModel>(); public ActionResult Index()
{
Random rd = new Random();
models.Add(new TestModel() { Name = "test1", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
models.Add(new TestModel() { Name = "test" + rd.Next(, ) + "", Age = rd.Next(, ), Remark = "Remark" });
return View();
} public PartialViewResult PagedListFun(string searchStr, string orderType, int? page)
{
if (!string.IsNullOrWhiteSpace(searchStr))
{
page = ;
} string str = JsonConvert.SerializeObject(models); string jsonstr = str; IList<TestModel> list = JsonConvert.DeserializeObject<IList<TestModel>>(jsonstr); var res = from q in list
where q.Name.Contains(searchStr ?? "")
select q; //if (orderType.ToLower() == "desc")
//{
res = res.OrderByDescending(o => o.Name);
//} int pageSize = ;
int pageNumber = (page ?? ); return PartialView("PagedListFun", res.ToPagedList(pageNumber, pageSize));
} }
}

6. 在index中加载页面

@{
Layout = null;
ViewBag.Title = "Index";
} <script src="~/Scripts/jquery-1.8.2.js"></script> <h2>Index</h2> <div id="partTime"></div> <script type="text/javascript"> $(function () {
$.ajax({
url: "@Url.Action("PagedListFun")",
type: "get",
data: {name:'mxh'},
success: function (data) {
$("#partTime").html(data);
},
error:function(error) {
$("#partTime").html(error.text);
}
}); }); </script>

7.在局部也中进行分页处理

@model PagedList.IPagedList<MvcApplication2.Models.TestModel>
@using PagedList.Mvc <link href="~/Content/PagedList.css" rel="stylesheet" />@*一定要加入这个,不然没有样式*@
@{
Layout = null;
ViewBag.Title = "PagedListFun";
} <ul>
@foreach (var item in Model)
{
<li>@item.Name @item.Age @item.Remark</li>
}
</ul> Page @(Model.PageCount < Model.PageNumber ? : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("PagedListFun", new { page }))

MVC 使用PageList进行分页的更多相关文章

  1. 【.NET MVC分页】.NET MVC 使用pagelist 分页

    1.安装 2. 在NuGet程序包管理控制台 ,输入Install-Package PagedList.mvc 安装PagedList 和PageList.Mvc; 3. @{ Layout = nu ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(8)-MVC与EasyUI DataGrid 分页

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI Datagrid在加载的时候会提交一些分页的信息到后台,我们需要根据这些信息来进行数据分页再次返回到前台 实 ...

  3. MVC 使用Jquery EasyUI分页成功

    先上图吧

  4. MVC中的自定义控件——分页

    上一篇是个简单例子,这篇借分页写个升级版的例子. 不想拼html代码,因为不好修改,那写一个PartialView. @model System.Web.Mvc.PagerModel @if (Mod ...

  5. MVC如何使用开源分页插件shenniu.pager.js

    最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...

  6. MVC默认路由实现分页-PagerExtend.dll

    这两天在群里有人咨询有没有现成的.net mvc分页方法,由此写了一个简单分页工具,这里简单分享下实现思路,代码,希望能对大家有些帮助,鼓励大家多造些轮子还是好的. A.效果(这里用了bootstra ...

  7. MVC使用x.PagedList分页

    MVC分页 1.Install Package Tools=> NuGet Package Manager=>Manager NuGet Packages of Solution

  8. MVC 下 使用MvcPager分页控件

    先去下载 mvcpage http://mvcpager.codeplex.com/releases/view/64098只需要一个 MvcPager.dll文件即可 然后引用MvcPager.dll ...

  9. asp.net mvc 的几种分页Pager

    第一种 /// <summary> /// 分页Pager显示 /// </summary> /// <param name="html">&l ...

随机推荐

  1. [Uva247][Tarjan求强连通分量][Calling Circles]

    题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...

  2. python 笔记2--函数

    函数变量 >>> a = abs # 变量a指向abs函数 >>> a(-1) # 所以也可以通过a调用abs函数 1 定义函数 def my_abs(x): if ...

  3. linux进程间通信之管道篇

    本文是对http://www.cnblogs.com/andtt/articles/2136279.html管道一节的进一步阐释和解释 1 管道 1.1 管道简介 管道是unix系统IPC的最古老的形 ...

  4. MVC之MVCSQO方法查询、排序、分页、投影

  5. ios app相互调用

    被调用app配置 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NS ...

  6. Windows Server 2008 R2 IIS重装

    背景描述: 在一个刚睡醒午觉后的下午,忽然收到客户反馈,说昨天开始应用特别卡,各种卡各种不好用,忽然想到上次说要优化服务器IIS配置还一直没弄,然后迷迷糊糊的就开始进行客户现场服务器IIS配置优化,涉 ...

  7. windows驱动编程(目录)

    目录 第一章 入门 配置开发环境 第一个程序 应用程序调用内核函数的流程

  8. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  9. 单光纤udp通信

    环境:      两块板子,拥有独立系统(Linux),通过单光纤连接(数据只能单向发送,无反馈).两块板子采用udp协议通信. 问题: 发送板子发送数据后,接收板子上的进程收不到数据. 确认两块光纤 ...

  10. Oracle学习之常见错误整理

    一.ORA-12154: TNS: 无法解析指定的连接标识符 在程序中连接Oracle数据库的方式与其他常用数据库,如:MySql,Sql Server不同,这些数据库可以通过直接指定IP的方式连接, ...