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. QT中窗口刷新事件的学习总结

    一.主要理解一下几个方法和属性: 1.QWidget * QScrollView::viewport () const 2.void QWidget::paintEvent ( QPaintEvent ...

  2. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  3. 【每天一个Linux命令】19. 创建文件夹目录命令mkdir

    命令用途 mkdir 命令用来创建指定的名称的目录 使用说明 1.  创建目录的用户在当前目录中具有写权限 2. 指定的目录名不能是当前目录中已有的目录. 命令实例 0. 帮助文件 bixiaopen ...

  4. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  5. poj1014 Dividing (多重背包)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 Descrip ...

  6. Linux应用开发环境搭建

    因为笔者是一名大学生,对Linux内核开发方向非常感兴趣,可是实在是能(ji)力(shu)有(cha)限(jin),仅仅能从Linux应用开发開始,由浅入深,逐步进步,登上人生高峰,因此,昨天搭建了开 ...

  7. leetcode_question_57 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  9. 关于transform的2D

    在transform的学习中,自己总结了一点经验. 我们知道transform有2D和3D的两大类变换,这里分享下关于2D的属性简单示例: 一.2D变换:  (x为水平,y为垂直) 1.skew(斜拉 ...

  10. CSS transform(变形)和transform-origin(变形原点)

    transform(变形)和transform-origin(变形原点)的说明: 目前这两个属性得到了除去ie以外各个主流浏览器webkit,firefox,opera的支持,属性名分别为 -webk ...