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());
}
} }
}

最新文章

  1. ifconfig: command not found 如何解决?
  2. Ubuntu安装Gnome3
  3. MYSql存储过程的作用及语法
  4. jQuery ajax 返回的数据类型
  5. TermServDevices报错导致服务器死机(远程服务使用者必读)
  6. jquery让一个点击事件刷新页面就自己执行一次的方法
  7. Android开发之如何保证Service不被杀掉(前台服务)
  8. C# json to dynamic object
  9. iOS 点击cell下拉
  10. 深入C语言内存区域分配(进程的各个段)详解(转)
  11. Spring AOP高级——源码实现(3)AopProxy代理对象之JDK动态代理的创建过程
  12. 逆向学习-PE文件格式
  13. 002.MySQL高可用主从复制部署
  14. poj 3177 Redundant Paths(边双连通分量+缩点)
  15. Java微信二次开发(四)
  16. noip第21课作业
  17. [转]table中设置tr行间距
  18. 安全检测:2013 Top 10 List
  19. Jekens 配置多项目SCM GitLab+Jenkins持续集成环境
  20. SQL中的替换函数replace()使用

热门文章

  1. Android android.support.v7.appcompat.R$styleable
  2. 使用ngrok让微信公众平台通过80端口访问本机
  3. UML--对象的介绍
  4. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场
  5. autoitv3点击windows界面
  6. 工作中用到的linux命令
  7. python核心编程第二版笔记
  8. javascript 中 &quot;!function(){}() &quot; 是什么意思?
  9. read(),write() 读/写文件
  10. verilog 实现加法器