<div class="text-center">
    <span style="display:inline-block;  position:relative;top:-30px;">共 @Model.TotalPageCount 页 @Model.TotalItemCount 条记录,当前为第 @Model.CurrentPageIndex 页</span>
    @Ajax.Pager(Model, new PagerOptions
    {
        /*
         * 设置分页显示的样式
         */
        FirstPageText = "首页",
        LastPageText = "最后一页",
        NextPageText = "下一页",
        PrevPageText = "上一页",
        PageIndexParameterName = "PageId",
        ContainerTagName = "ul",
        CssClass = "pagination",
        CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>",
        DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>",
        PagerItemTemplate = "<li>{0}</li>"
    },
        new MvcAjaxOptions
        {
            HttpMethod = "post",
            UpdateTargetId = "partList",//更新数据的标签ID
            DataFormId = "form0"       //分页对应的条件搜索表单ID
        })
</div>

这是前台页面,直接复制到前台最下面即可!

/* MvcPager source code
This file is part of MvcPager.
Copyright 2009-2015 Webdiyer(http://en.webdiyer.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
var Webdiyer = Webdiyer || {};
Webdiyer.MvcPagers = [];
Webdiyer.MvcPagers.getById = function (id) {
    for (var i = 0; i <= this.length; i++) {
        if (typeof this[i] !== "undefined" && this[i].id === id) {
            return this[i];
        }
    }
    return null;
};
Webdiyer.__ajaxPages = {}; //Ajax page parameters, preventing multiple MvcPager with same UrlPageIndexName trigger multiple http requests
Webdiyer.MvcPager = function (wrapper) {
    this.wrapper = wrapper;
};
Webdiyer.MvcPager.prototype = {
    wrapper: null,
    id: null,
    urlFormat: null,
    pageIndexName: null,
    updateTarget: null,
    onBegin: null,
    onComplete: null,
    onFailure: null,
    onSuccess: null,
    httpMethod: null,
    confirm: null,
    loadingElementId: null,
    loadingDuration: 0,
    partialLoading: null,
    currentPageIndex: null,
    dataFormId: null,
    allowCache: true,
    enableHistorySupport: null,
    //autoSubmitForm: null,
    searchCriteria: null,
    pageCount: null,
    invalidPageErrMsg: null,
    outOfRangeErrMsg: null,
    firstPageUrl: null,
    pageIndexBox: null,
    goToButton: null,
    maxPageIndexItems: 20,
    isAjaxPager: null,
    onError: null,
    isFirstLoading: true,
    allowReload: false, //used by search form to check if reload is needed even current page index is not changed
    init: function () {
        var wrapper = $(this.wrapper);
        var newPageIndex = 1;
        var initialPageIndex; //value of the page index when page is loaded first time, if go back to this page we need to load the correct data by checking this value(it's not necessarily to be 1)
        this.id = wrapper.attr("id");
        this.isAjaxPager = wrapper.data("ajax") || false;
        this.pageCount = wrapper.data("pagecount");
        this.invalidPageErrMsg = wrapper.data("invalidpageerrmsg");
        this.outOfRangeErrMsg = wrapper.data("outrangeerrmsg");
        this.firstPageUrl = wrapper.data("firstpage");
        this.urlFormat = wrapper.data("urlformat");
        this.pageIndexName = wrapper.data("pageparameter");
        this.currentPageIndex = wrapper.data("currentpage") || 1;
        this.pageIndexBox = wrapper.data("pageindexbox");
        this.goToButton = wrapper.data("gotobutton");
        this.maxPageIndexItems = wrapper.data("maxitems") || 20;
        this.onError = wrapper.data("onerror") || "alert(errMsg)";
        var context = this;
        if (this.isAjaxPager) {
            this.updateTarget = wrapper.data("ajax-update");
            this.onBegin = wrapper.data("ajax-begin");
            this.onComplete = wrapper.data("ajax-complete");
            this.onFailure = wrapper.data("ajax-failure");
            this.onSuccess = wrapper.data("ajax-success");
            this.confirm = wrapper.data("ajax-confirm") || undefined;
            this.httpMethod = wrapper.data("ajax-method") || "GET";
            this.loadingElementId = wrapper.data("ajax-loading") || undefined;
            this.dataFormId = wrapper.data("ajax-dataformid") || undefined;
            this.allowCache = wrapper.data("ajax-allowcache") || true;
            var history = wrapper.data("ajax-enablehistorysupport");
            this.enableHistorySupport = (typeof history === "undefined" ? true : history);
            //this.autoSubmitForm = wrapper.data("ajax-autosubmitform") || false;
            this.loadingDuration = wrapper.data("ajax-loading-duration") || 0;
            this.partialLoading = wrapper.data("ajax-partialloading") || false;
            initialPageIndex = this.currentPageIndex;
            var pagerSelector = "[data-pagerid='Webdiyer.MvcPager']";
            var hashIndex = this.__getPageIndex(this.pageIndexName);
            if (hashIndex !== this.currentPageIndex && hashIndex > 0)
            { this.__ajax(hashIndex, { type: this.httpMethod, data: [] }); }
            if (typeof this.dataFormId !== "undefined") {
                var isAjaxForm = $(context.dataFormId).data("ajax") || false;
                $(context.dataFormId).submit(function (event) {
                    context.searchCriteria = $(context.dataFormId).serializeArray();
                    if (isAjaxForm) {
                        if (context.currentPageIndex !== 1) {
                            context.currentPageIndex = 1;
                            if (context.enableHistorySupport) {
                                context.__setPageIndex(context.pageIndexName, -1); //prevent reloading triggered by hashchange event
                                context.allowReload = true;
                            } else {
                                context.__ajax(1, { type: context.httpMethod, data: [] });
                            }
                        } else {
                            if (typeof Webdiyer.__ajaxPages[context.pageIndexName] === "undefined")
                            { context.allowReload = true; }
                        }
                    } else {
                        if (typeof Webdiyer.__ajaxPages[context.pageIndexName] === "undefined")
                        { context.allowReload = true; }
                        if (context.currentPageIndex === 1) {
                            context.__ajax(1, { type: context.httpMethod, data: [] });
                        } else {
                            if (context.enableHistorySupport) {
                                context.__setPageIndex(context.pageIndexName, 1);
                            } else {
                                context.__ajax(1, { type: context.httpMethod, data: [] });
                            }
                            context.currentPageIndex = 1;
                        } context.allowReload = true;
                        event.preventDefault();
                    }
                });
            }
            if (this.enableHistorySupport) {
                this.__initHashChange(initialPageIndex);
            }
            //pagination items
            $(this.updateTarget).on("click", pagerSelector + " a[data-pageindex]", function (e) {
                newPageIndex = $(this).data("pageindex");
                e.preventDefault();
                if (context.enableHistorySupport) {
                    context.__setPageIndex(context.pageIndexName, newPageIndex);
                } else {
                    context.__ajax(newPageIndex, { type: context.httpMethod, data: [] });
                }
            });
        }
        //page index box
        this.__bindPageIndexBox();
        return this;
    },
    __bindPageIndexBox: function () {
        var context = this;
        if (context.isAjaxPager) {
            if ($.trim(context.pageIndexBox) !== "") {
                if ($(context.updateTarget).find(context.pageIndexBox).length > 0) {
                    //page index box is inside of update target area
                    //check if page index box is dropdownlist and fill it with page indices
                    context.__fillPageIndexBox();
                    if ($(context.pageIndexBox).is('input:text')) {
                        $(context.pageIndexBox).val(context.currentPageIndex);
                        $(context.updateTarget).on("keydown", context.pageIndexBox, function () {
                            context.__validateInput(event);
                        });
                    }
                    if ($.trim(context.goToButton) !== "") {
                        $(context.updateTarget).on("click", context.goToButton, function () {
                            var newPageIndex = $(context.pageIndexBox).val();
                            context.goToPage(newPageIndex);
                        });
                    } else {
                        $(context.updateTarget).on("change", context.pageIndexBox, function () {
                            var newPageIndex = $(context.pageIndexBox).val();
                            context.goToPage(newPageIndex);
                        });
                    }
                } else {
                    //page index box is outside of update target area
                    context.__bindBoxEvents();
                }
            }
        } else {
            //page index box
            if ($.trim(context.pageIndexBox) !== "") {
                context.__bindBoxEvents();
            }
        }
    },
    __bindBoxEvents: function () {
        var context = this;
        context.__fillPageIndexBox();
        if ($(context.pageIndexBox).is('input:text')) {
            $(context.pageIndexBox).val(context.currentPageIndex);
            $(context.pageIndexBox).keydown(function () {
                context.__validateInput(event);
            });
        }
        if ($.trim(context.goToButton) !== "") {
            $(context.goToButton).click(function () {
                var newPageIndex = $(context.pageIndexBox).val();
                context.goToPage(newPageIndex);
            });
        } else {
            $(context.pageIndexBox).change(function () {
                var newPageIndex = $(context.pageIndexBox).val();
                context.goToPage(newPageIndex);
            });
        }
    },
    __fillPageIndexBox: function () {
        var se = $(this.pageIndexBox);
        if (se.prop('type') === 'select-one') {
            se.empty();
            var startIndex = this.currentPageIndex - (this.maxPageIndexItems / 2);
            if (startIndex + this.maxPageIndexItems > this.pageCount)
            { startIndex = this.pageCount + 1 - this.maxPageIndexItems; }
            if (startIndex < 1)
            { startIndex = 1; }
            var endIndex = startIndex + this.maxPageIndexItems - 1;
            if (endIndex > this.pageCount)
            { endIndex = this.pageCount; }
            for (var i = startIndex; i <= endIndex; i++) {
                se.append('<option value="' + i + '">' + i + '</option>');
            }
        }
        se.val(this.currentPageIndex);
    },
    __initHashChange: function (initialPageIndex) {
        var context = this;
        var docMode = document.documentMode;
        if ("onhashchange" in window &&
        (docMode === undefined || docMode > 7)) //IE compatable mode
        {
            $(window).bind("hashchange", function () {
                var pageIndex = context.__getPageIndex(context.pageIndexName);
                if (pageIndex === 0)
                { pageIndex = initialPageIndex; } //initial page index in url without hash value,eg. when go back from articles/list/3#id=2 to articles/list/3 initialPageIndex will be 3;
                context.__ajax(pageIndex, { type: context.httpMethod, data: [] });
            });
        } else {
            var currentHash = window.location.hash;
            setInterval(function () {
                var pageIndex = context.__getPageIndex(context.pageIndexName);
                if (window.location.hash !== currentHash) {
                    currentHash = window.location.hash;
                    if (pageIndex === 0)
                    { pageIndex = initialPageIndex; }
                    context.__ajax(pageIndex, { type: context.httpMethod, data: [] });
                }
            }, 200);
        }
    },
    __getPageIndex: function (pname) {
        var hash = window.location.hash.substring(1);
        if ($.trim(hash) !== "") {
            var harr = hash.split('&');
            for (var i = 0; i < harr.length; i++) {
                var hval = harr[i].split("=");
                if (hval[0].toString().toLowerCase() === pname.toString().toLowerCase()) {
                    return parseInt(hval[1]) || 1;
                }
            }
        }
        return 0;
    },
    __setPageIndex: function (pname, pindex) {
        var hash = window.location.hash.substring(1);
        if ($.trim(hash) === "")
        { window.location.hash = pname + "=" + pindex; }
        else {
            var r = new RegExp(pname + "=[^&]*", 'i');
            if (!r.test(hash))
            { window.location.hash += "&" + pname + "=" + pindex; }
            else {
                var index = hash.replace(r, pname + "=" + pindex);
                window.location.hash = index;
            }
        }
    },
    goToPage: function (pageIndex) {
        var r = new RegExp("^\\s*(\\d+)\\s*$");
        if (!r.test(pageIndex)) {
            this.__getFunction(this.onError, ["errType", "errMsg"]).apply(this, [0, this.invalidPageErrMsg]);
            return;
        } else if (RegExp.$1 < 1 || RegExp.$1 > this.pageCount) {
            this.__getFunction(this.onError, ["errType", "errMsg"]).apply(this, [1, this.outOfRangeErrMsg]);
            return;
        }
        if (this.isAjaxPager) {
            if (this.enableHistorySupport) {
                this.__setPageIndex(this.pageIndexName, pageIndex);
            } else {
                this.__ajax(pageIndex, { type: this.httpMethod, data: [] });
            }
        } else {
            if (typeof this.firstPageUrl !== "undefined" && this.firstPageUrl !== false && parseInt(pageIndex) === 1)
            { window.self.location.href = this.firstPageUrl; }
            else
            { window.self.location.href = decodeURI(this.urlFormat).replace("__" + this.pageIndexName + "__", pageIndex); }
        }
    },
    __ajax: function (index, options) {
        var context = this;
        if (typeof Webdiyer.__ajaxPages[context.pageIndexName] === "undefined" || Webdiyer.__ajaxPages[context.pageIndexName] !== index || context.allowReload) { //prevent duplicate ajax requests
            if (index === -1 || (index === 0 && context.isFirstLoading) || (index === context.currentPageIndex && !context.allowReload)) {
                return;
            }
            if (context.confirm && !(window.confirm(context.confirm))) {
                return;
            }
            $.extend(options, {
                url: this.urlFormat.replace("__" + context.pageIndexName + "__", index),
                cache: this.allowCache,
                beforeSend: function (xhr) {
                    var formMethod = options.type.toUpperCase();
                    if (!(formMethod === "GET" || formMethod === "POST")) {
                        xhr.setRequestHeader("X-HTTP-Method-Override", formMethod);
                    }
                    var result = context.__getFunction(context.onBegin, ["xhr"]).apply(this, arguments);
                    if (result !== false && typeof context.loadingElementId !== "undefined") {
                        $(context.loadingElementId).show(context.loadingDuration);
                    }
                    return result; //Ajax request will be cancelled if return false
                },
                complete: function () {
                    if (typeof context.loadingElementId !== "undefined") {
                        $(context.loadingElementId).hide(context.loadingDuration);
                    }
                    context.__fillPageIndexBox();
                    context.__getFunction(context.onComplete, ["xhr", "status"]).apply(this, arguments);
                },
                success: function (data) {
                    if (context.partialLoading)
                    { $(context.updateTarget).html($(context.updateTarget, data).html()); }
                    else
                    { $(context.updateTarget).html(data); }
                    context.currentPageIndex = index;
                    context.isFirstLoading = false;
                    context.__getFunction(context.onSuccess, ["data", "status", "xhr"]).apply(this, arguments);
                },
                error: context.__getFunction(context.onFailure, ["xhr", "status", "error"])
            });
            if (typeof context.dataFormId !== "undefined") {
                context.__pushData(options.data, context.searchCriteria);
            }
            options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
            var method = options.type.toUpperCase();
            if (!(method === "GET" || method === "POST")) {
                options.type = "POST";
                options.data.push({ name: "X-HTTP-Method-Override", value: method });
            }
            $.ajax(options);
            Webdiyer.__ajaxPages[context.pageIndexName] = index;
        }
    },
    __pushData: function (dataArr, dataToPush) {
        if (dataToPush !== null && typeof dataToPush !== "undefined") {
            for (var i = 0; i < dataToPush.length; i++) {
                dataArr.push({ name: dataToPush[i].name, value: dataToPush[i].value });
            }
        }
    },
    __getFunction: function (code, argNames) {
        var fn = window, parts = (code || "").split(".");
        while (fn && parts.length) {
            fn = fn[parts.shift()];
        }
        if (typeof (fn) === "function") {
            return fn;
        } //onSuccess="functionName"
        if ($.trim(code).toLowerCase().indexOf("function") === 0) {
            /*jslint evil: true */
            return new Function("return (" + code + ").apply(this,arguments);");
        } //onSuccess="function(data){alert(data);}"
        argNames.push(code);
        try {
            return Function.constructor.apply(null, argNames); //onSuccess="alert('hello');return false;"
        } catch (e) {
            alert("Error:\r\n" + code + "\r\n is not a valid callback function");
        }
    },
    __validateInput: function (e) {
        var kc;
        if (window.event) {
            kc = e.keyCode;
        }
        else if (e.which) {
            kc = e.which;
        }
        var valideKeys = [8, 37, 39, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105];
        if (kc !== null && valideKeys.indexOf(kc) < 0) {
            if (e.preventDefault) {
                e.preventDefault();
            }
            else {
                event.returnValue = false;
            }
        }
    }
};
(function ($) {
    $.fn.initMvcPagers = function () {
        return this.each(function () {
            Webdiyer.MvcPagers.push(new Webdiyer.MvcPager(this).init());
        });
    };
})(jQuery);
$(function () { $("[data-pagerid='Webdiyer.MvcPager']").initMvcPagers(); });
这是JS代码 封装起来,页面直接引用!
 
 
 
 
 
还要引用一个程序集,在我博客里面,这里不能放,自己去看看吧!

ASP.NETMVC 分页的更多相关文章

  1. asp.net分页控件

    一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Reg ...

  2. .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)

    阅读目录: 1.需求背景介绍(Model元数据设置项应该与View绑定而非ViewModel) 1.1.确定问题域范围(可以使用DSL管理问题域前提是锁定领域模型) 2.迁移ViewModel设置到外 ...

  3. .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(三)

    阅读目录: 7.HtmlHelper.HtmlHelper<T>中的ViewModel的类型推断 8.控制ViewModel中的某个属性的呈现(使用PartialView部分视图细粒度控制 ...

  4. 一步一步搭框架(asp.netmvc+easyui+sqlserver)-03

    一步一步搭框架(asp.netmvc+easyui+sqlserver)-03 我们期望简洁的后台代码,如下: using System; using System.Collections.Gener ...

  5. 一步一步搭框架(asp.netmvc+easyui+sqlserver)-02

    一步一步搭框架(asp.netmvc+easyui+sqlserver)-02 我们期望简洁带前台代码,如下: <table id="dataGrid" class=&quo ...

  6. 一步一步搭框架(asp.netmvc+easyui+sqlserver)-01

    一步一步搭框架(asp.netmvc+easyui+sqlserver)-01 要搭建的框架是企业级开发框架,适用用企业管理信息系统的开发,如:OA.HR等 1.框架名称:sampleFrame. 2 ...

  7. asp.net 分页-自己写分页控件

    去年就发表过asp.net 分页-利用后台直接生成html分页 ,那种方法只是单纯的实现了分页,基本不能使用,那时就想写个自己的分页控件,无奈能力有限.最近有点时间了,就自己做出了这个分页控件.我承认 ...

  8. Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表。

    Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表,服务器需要安装如下的补丁,才可正常访问. http://www.microsoft.com ...

  9. (转)asp.net分页存储过程

    Asp.Net分页存储过程 SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (sel ...

随机推荐

  1. C# 操作Word目录——生成、删除目录

    目录,是指书籍.文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读.检索内容的作用.在Word中生成目录前,需要设置文档相应文字或者段落的大纲级别,根据设定的大纲级别可创建文档的交互式大纲 ...

  2. 全球第一免费开源ERP Odoo PM OKR项目管理操作指南

    概览 ​ Odoo项目允许你和整个项目团队一起管理项目, 与项目和任务成员中的任何一个人沟通. 它与包含可定制阶段的任务的项目一起工作.项目可以是内部的或客户导向的.任务是项目执行的一部分.你可以给这 ...

  3. ArcGIS API for JavaScript 4.9 & 3.26 发布与新特性

    应该是中文首发?我只想说:更新太TMD快了 QAQ Part I -- JsAPI 4.9 主题1:在2D中默认启用WebGL渲染 在要素图层.CSV图层.流图层中是使用WebGL渲染的,这个任务由M ...

  4. Linux新加磁盘挂载和重启自动挂载

    提示两点:*新加的硬盘需要重启服务器fdisk -l才能看到*下面操作要用root账户大概是这样的,查看-分区-格式化-挂载-重启自动挂载1.加硬盘后重启服务器查看[root@test199 ~]# ...

  5. 三、 redis进阶篇

    1. redis事务 使用方法:方法为先发送multi命令告诉redis,下面所有的命令属于同一个事务,先不要执行,而是把他们暂时存起来,redis返回OK,然后后面执行需要放在同一个事务里的命令,可 ...

  6. Windows Server 2016-Netdom Join之客户端加域(二)

    上章节我们介绍了如何通过图形化界面实现用户加域操作,本章节为大家简单介绍如何通过netdom join命令实现计算机加域操作.具体信息如下: 将工作站或成员服务器加入域.将计算机加入域的行为会为域中的 ...

  7. Video/Audio禁止快进(退)

    首先接着上个随笔.上个随笔主要介绍了视频音频的相关操作.属性和方法.这里主要记录一个应用:禁止快进(快退同理). 思路:监听快进事件(此处是监听播放时间更新),利用一个缓存的时间和播放到的时间进行对比 ...

  8. Leetcode 136.只出现一次的数字 By Python

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  9. 2018~试试idea~

    无聊~随便写了一个 输入控制台,发现点击run之后,反应要比eclipse慢一些,不知道是什么原因!!! 略略的操作了一下,还是和eclipse的使用习惯有很大的差异~

  10. SUSE12SP3-Mycat(4)rule.xml配置详解

    简介 rule.xml 里面就定义了我们对表进行拆分所涉及到的规则定义.我们可以灵活的对表使用不同的分片算法, 或者对表使用相同的算法但具体的参数不同.这个文件里面主要有 tableRule 和 fu ...