1. NuGet 下载 PagedList.MVC

2. View Page

@model PagedList.IPagedList<Libaray.Models.Entities.BookModel>
@using PagedList.Mvc;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
} <script type="text/javascript">
$(function () { $("#txtSearch").val(@Request.QueryString["keyWords"]); })
</script> <div class="row" style="margin-top:20px;">
<div class="col-sm-2">
@Html.Partial("_AccountNavigator")
</div>
<div class="col-sm-10">
<div class="row">
<div class="col-sm-6">
<a class="btn btn-sm btn-primary" href="/Book/NewBook">新增</a>
</div>
<div class="col-sm-6 ">
<form id="formSearch" method="get" class="form-horizontal">
<div class="input-group text-right">
@Html.TextBox("keyWords",ViewBag.KeyWords as string, new { @name= "keyWords",@class = "form-control", @placeholder = "书名/作者/描述..." })
<div class="input-group-btn">
<button id="search" class="btn btn-sm btn-primary">查询</button>
</div>
</div>
</form>
</div>
</div> <hr />
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>图书名称</th>
<th>作者</th>
<th>描述</th>
<th>生产日期</th>
<th>录入时间</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(m => item.BookName)</td>
<td>@Html.DisplayFor(m => item.Author)</td>
<td>@Html.DisplayFor(m => item.BookDescription)</td>
<td>@Html.DisplayFor(m => item.BookDate)</td>
<td>@Html.DisplayFor(m => item.CreatedOn)</td>
<td><a href="/Book/NewBook?BookId=@item.BookId">编辑</a></td>
</tr>
}
</tbody>
</table>
@Html.PagedListPager(Model, id => Url.Action("BookList", new { id,keyWords=ViewBag.KeyWords }))
</div>
</div>
</div>

2. Book Controller

 // GET: Book
public ActionResult BookList(int id=,int irowCount=, string keyWords =null)
{
BookService BookDAL = new BookService();
ViewBag.KeyWords = keyWords;
var sResult = BookDAL.SearchBookList(id, irowCount, keyWords);
return View(sResult);
}
public class BookService:BaseService<BookModel> //此处用到了Cache, 如果不需要则可跳过,
{
public IPagedList<BookModel> SearchBookList(int id = , int irowCount=, string sKeyWords = null)
{
List<BookModel> sList = new List<BookModel>();
if (WebCacheHelper.GetCache("BookList") == null)
{
using (LibContext = new LibarayContext())
{
sList = LibContext.BookModels.ToList();
WebCacheHelper.SetCache("BookList", sList);
}
}
else
{
sList = WebCacheHelper.GetCache("BookList") as List<BookModel>;
} if (!string.IsNullOrEmpty(sKeyWords))
{
var squery = from bk in sList where bk.Author.Contains(sKeyWords) || bk.BookName.Contains(sKeyWords) || bk.BookDescription.Contains(sKeyWords) select bk; //注意,如果Author, BookName, Description 为null, 此行会报错。 return squery.OrderByDescending(u => u.UpdatedOn).ToPagedList(id, irowCount);
}
else
{
return sList.OrderByDescending(m => m.UpdatedOn).ToPagedList(id, irowCount);
}
}
}

WebCacheHelper // 转载别人,具体出处忘记了。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace Libaray.Common
{
public class WebCacheHelper
{
/// <summary>
/// 缓存辅助类
/// </summary> /// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
} /// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
} /// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
} }
}

PagedList.MVC 应用的更多相关文章

  1. ASP.NET MVC 4使用PagedList.Mvc分页

    ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...

  2. 如何使用 PagedList.Mvc 分页

    刚开始找PagedList分页不是例子太复杂,就是写的过于简略,由于对于MVC的分页不太了解,之前使用的都是Asp.Net 第三方控件 + 数据库存储过程分页.还是老外写的例子简捷,https://g ...

  3. PagedList.MVC分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. Mvc 自带分页控件PagedList.Mvc Demo示例

    添加/下载PagedList.Mvc 直接搜索mvc pagelist 就会出来.安装完成即可.在项目的packages文件夹下面就会出现PagedList.Mvc.4.5.0.0 和PagedLis ...

  5. MVC+Bootstrap+Drapper使用PagedList.Mvc支持多查询条件分页

    前几天做一个小小小项目,使用了MVC+Bootstrap,以前做分页都是异步加载Mvc部分视图的方式,因为这个是小项目,就随便一点.一般的列表页面,少不了有查询条件,下面分享下Drapper+Page ...

  6. asp.net MVC 使用PagedList.MVC实现分页

    在上一篇的EF之DB First中,存在以下的两个问题: 1. 添加/编辑页面显示的是属性名称,而非自定义的名称(如:姓名.专业...) 2. 添加/编辑时没有加入验证 另外数据展示使用分页 @Htm ...

  7. PagedList.Mvc只有一行时不显示分页

    PagedList.Mvc默认总是显示分页,可以通过设置DisplayMode在只有一行时不显示分页 @Html.PagedListPager(Model, page => Url.Action ...

  8. 再谈使用X.PagedList.Mvc 分页(ASP.NET Core 2.1)

    在以前的博文中写过使用X.PagedList.Mvc组件来对ASP.NET MVC应用程序进行分页,可以参考此篇随笔:Asp.net MVC 使用PagedList(新的已更名 为X.PagedLis ...

  9. Asp.net MVC 使用PagedList(新的已更名 为X.PagedList.Mvc) 分页

    在asp.net mvc 中,可以bootstrap来作为界面,自己来写分页程序.也可以使用PagedList(作者已更名为 X.PagedList.Mvc)来分页. 1.首先,在NuGet程序包管理 ...

随机推荐

  1. Some General concepts in ISO C

    [ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...

  2. Spring中Bean的命名问题及ref和idref之间的区别

    一直在用Spring,其实对其了解甚少,刚去了解了一下Spring中Bean的命名问题以及ref和idref之间的区别,略作记录,以备后查. Spring中Bean的命名 1.每个Bean可以有一个i ...

  3. Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程

    网络爬虫是捜索引擎抓取系统的重要组成部分.爬虫的主要目的是将互联网上的网页下载到本地形成一个或联网内容的镜像备份. 一个通用的网络爬虫的框架如图所示:

  4. bzoj1190

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1190 神题...... F[i][j]表示容量为j*2^i+W第i-1位到第0位的最大价值, ...

  5. WPF 实现控件间拖拽内容

    想实现这样一个常用功能:在ListBox的一个Item上点住左键,然后拖拽到另外一个控件(如ListView中),松开左键,数据已经拖拽过来. 步骤如下: 1. 设置ListBox 的AllowDro ...

  6. java获取项目地址或tomcat绝对地址

    在java项目中获取文件的路径,不管是相对路径还是绝对路径,其本质都是通过绝对路径去寻找. 获取项目地址 request.getSession().getServletContext().getRea ...

  7. (转)iOS7界面设计规范(8) - UI基础 - 术语和措辞

    讨厌周一,讨厌一周.今天中午交互组聚餐,却很开心:大家都是很厉害的人,你可以感到他们身上的能量,可以感到有些什么东西正在推着自己尽力向前走.这是一种很健康的状态,同时也很难得,自然越发需要珍惜.从无到 ...

  8. [置顶] cJSON库(构建json与解析json字符串)-c语言

     一.c语言获取json中的数据. 1.先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h. 2.感性认识 char * json = "{ \"json\" ...

  9. 实战ffs函数

    这个函数是返回整形的最低位1的位置 自己写是这个样子的: /* Find the first bit set in I. */ int lx_ffs(int i) { int index = 0, r ...

  10. Picasso – Android系统的图片下载和缓存类库

    Picasso – Android系统的图片下载和缓存类库 Picasso 是Square开源的一个用于Android系统下载和缓存图片的项目.该项目和其他一些下载图片项目的主要区别之一是:使用4.0 ...