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. Ruby的语法糖

    发现Ruby的语法糖好多,比如函数调用,参数列表可以写括号和不写括号.代码块可以用do end 或者 {}.   还有 if,unless后置.等等. 如果看Ruby代码看多了,你会发现,它很多地方的 ...

  2. Linux企业级项目实践之网络爬虫(24)——定制规则扩展为垂直爬虫

    在垂直搜索的索引建立之前,我们需要到垂直网站上抓取资源并做一定的处理.垂直搜索与通用搜索不同之处在于,通用搜索不需要理会网站哪些资源是需要的,哪些是不需要的,一并抓取并将其文本部分做索引.而垂直搜索里 ...

  3. Ajax--json(Ajax调用返回json封装代码、格式及注意事项)

    Ajax调用json封装代码<dbda.php>: //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb ...

  4. the process cannot access the file because it is being used by another process

    当在IIS中改动绑定的port号后启动时遇到例如以下错误,表明你的port号已经被占用了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmljMDIyOA ...

  5. DIV 与 Table 嵌套

    当然可以了.对于DIV定义表示一块可显示 HTML 的区域. Specifies a container that renders HTML. 注释此元素在 Internet Explorer 3.0 ...

  6. 图像重采样(CPU和GPU)

    1 前言 之前在写影像融合算法的时候,免不了要实现将多光谱影像重采样到全色大小.当时为了不影响融合算法整体开发进度,其中重采样功能用的是GDAL开源库中的Warp接口实现的. 后来发现GDAL War ...

  7. Ubuntu 12.04设置打开远程桌面登录1

    teamviewer_linux.deb sudo dpkg --install teamviewer_linux.deb

  8. js数组 函数

    js数组 filter(),map(),some(),every(),forEach(),lastIndexOf(),indexOf() 文章1:http://www.jb51.net/article ...

  9. SVG 路径(path)

    本文转自:https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths <path>元素是SVG基本形状中最强大的一个,它 ...

  10. sharding的基本思想和理论上的切分策略

    本文着重介绍sharding的基本思想和理论上的切分策略,关于更加细致的实施策略和参考事例请参考我的另一篇博文:数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示 一.基本思想 Sh ...