惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

博客园 - 小马过河MJ

Identity Server introspect 调用 /connect/introspect windows forget jenkins password. 转载(Asp.net Core 中试使用ZKWeb.System.Drawing) EFCore & Mysql migration on Production HTML to PDF pechkin Log4net 为MVC 添加下载权限 辞职 MiniProfiler Android Studio 设置LogCat 颜色 运用Swagger 添加WebAPI 文档 给现有MVC 项目添加 WebAPI Summernote async await 跨域调用WebApi MVC 伪静态 设置EntityFramework 在开发时自动更新数据库 MVC 自定义错误处理 SQL Server 索引结构及其使用(四)[转] SQL Server 索引结构及其使用(三)[转]
PagedList.MVC 应用
小马过河MJ · 2016-02-02 · via 博客园 - 小马过河MJ

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=1,int irowCount=25, 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 = 1, int irowCount=20, 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());
            }
        }

    }
}