MVC简易分页(Razor)

一、无数据提交

     第一步,建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:  
        public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
        {
            //int count = db.Product.Count();
            ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
            ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
            ViewBag.action = action;
            ViewBag.controller = controller;
            return PartialView();
        }
  传入四个参数:

    action:操作(要分页的视图的操作,默认为Index);

    controller:控制器;

    currentPage:当前页数;

    pageCount:数据总页数

    第二步:添加视图(PageIndex)

   @if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
    {
        <span>您好,当前没有数据显示!</span>
    }
    else
    {
        if (ViewBag.CurrentPage <= 10)
      {
      <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
        首页</a>|</span>
      }

else
    {
    <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
        首页</a>

<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">
        ...</a> </span>
 
    }
    for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
    {
        if (i <= 0)
        {
            continue;
        }
        if (i > ViewBag.PageCount)
        {
            break;
        }
    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">
        第 @i 页</a>|</span>
    }
    if (ViewBag.CurrentPage > 1)
    {
    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">
        上一页</a>|</span>
    }
    if (ViewBag.PageCount > ViewBag.CurrentPage)
    {
    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">
        下一页</a></span>
    }
    if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
    {
   
    <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
        尾 页</a>
    }
    else
    {
    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">
        ...</a></span>
    <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
        尾 页</a>
    }
    <span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
    </span>
    }
  第三步:操作的视图的控制器修改

  public ViewResult Index(int? pageIndex)
    {
      int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
        ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);

      //这里的是take,按照每页20个显示
      return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
    }

  第四步:页面调用(即最后一步)

    @Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

一般来说,数据都是变动的。

二、有数据提交

      第一步:建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
       public ActionResult PageIndexKey(int currentPage, int pageCount)
        {
            ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
            ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
            return PartialView();
        }

    第二步:建立分布视图

<script>
    $(function () {
        $("#pageingByForm a").click(function (event) {
            $("#pageIndex").val($(this).attr("pageIndex"));
            //$(this).parent("Form").submit();
            document.getElementsByTagName("Form").item(0).submit();
            event.preventDefault();
        });
    });
</script>
@Html.Hidden("pageIndex")
<div id="pageingByForm">
    @if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
    {
        <span>当前没有数据</span>
    }
    else
    {
        if (ViewBag.CurrentPage <= 10)
        {
        <span><a pageindex="1" href="#">首页</a>|</span>
        }

else
        {
        <span><a pageindex="1" href="#">首页</a>|</span>

<span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>
        }
        for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
        {
            if (i <= 0)
            {
                continue;
            }
            if (i > ViewBag.PageCount)
            {
                break;
            }
        <span><a pageIndex="@i" href="#">第 @i 页</a>|</span>
        }
        if (ViewBag.CurrentPage >1)
        {
        <span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">上一页</a>|</span>
        }
        if (ViewBag.PageCount > ViewBag.CurrentPage)
        {
        <span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">下一页</a></span>
        }
        if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
        {
        }
        else
        {
        <span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>
        <span><a pageIndex="@ViewBag.PageCount" href="#">尾 页</a></span>
        }
        <span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
        </span>
    }
</div>

  第三步:修改操作视图和控制器

  public ViewResult Index(int? pageIndex ,string search)
  {
  int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
   ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);
  return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
  }

  视图(页面调用):

@using (Html.BeginForm())
{

根据性别得到查询结果

性别: @Html.TextBox("sex")

<input type="submit" value="查询" />

@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

}

Example:

    //数据,一个list的集合

    List<string> s = new List<string>();

s.Add("张军");

ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0);

return View(s.Skip((pageInd - 1) * 20).Take(20));

    @Html.Action("PageIndex", "PageIndex",

    new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

时间: 2024-08-08 15:33:21

MVC简易分页(Razor)的相关文章

C# MVC分页,razor

IMVCPages interface IMVCPages { int GetItemsCount(); int GetPageSize(); int GetPagesCount(); /// <summary> /// 当前页面索引,用于分页 /// </summary> int CurrentPageIndex { get; set; } } View <div> 查询到 @Model.GetItemsCount() 条记录,共 @Model.GetPagesCou

asp.net mvc 简易通用自定义Pager实现分页

asp.net mvc 简易通用自定义Pager实现分页 Intro 一个WEB应用程序中经常会用到数据分页,本文将实现一个简单通用的分页组件,包含一个 PagerModel (用来保存页码信息),一个 HtmlHelper 的 Pager 扩展方法和一个 PagedListModel<T> 分页数据模型. PagerModel 分页模型 PagerModel 用来保存分页信息,代码实现如下: 1 /// <summary> 2 /// PagerModel 分页模型 3 ///

Mvc Linq 分页 参考PagedList

第一次写博客 写的不好各位大神多多包涵. 我的分页主要是针对Linq 分页来写的,针对IEnumerable<T>  和 IQueryable<T> 类型数据分页. 开始上代码 创建接口: public interface IPagedList { /// <summary> /// 总记录数 /// </summary> int TotalCount { get; set; } /// <summary> /// 总页数 /// </su

(转)Asp.Net Mvc视图引擎Razor介绍

Asp.Net Mvc视图引擎Razor介绍 1.Razor介绍 程序园原创,转载请注明:http://www.kwstu.com/ArticleView/dabaomvc_201408240820545275 1)ASP.NET MVC3 带来了一种新的名为Razor 的视图引擎,提供了下列优点: Razor 的语法简单且清晰,只需要最小化的输入 Razor 容易学习,语法类似于 C# 和 VB Visual Studio 对于 Razor 提供了智能提示和语法着色 Razor 视图不需要允许

MVC快速分页

.NET手记-ASP.NET MVC快速分页的实现 对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实现分页功能.但是今天我描述的是用基本的C#和html代码实现的分页,不借助任何第三方组件. 实现思路 这里的实现主要借助ViewModel和HtmlHelper实现,通过传递分页参数PagingInfo来实现. 创建分页参数类PagingInfo.cs using System; namespace C

MVC中分页的实现

我在格斗人网 (www.helpqy.com) 中使用了下面的分页技术. 分页可以采用troygoode提供的开源包,其开源网站主页为:https://github.com/TroyGoode/PagedList.具体使用方法如下所示: 1. 通过NuGet下载PagedList.Mvc包,这个包会自动下载另外一个包PagedList,如下所示: 2. 在controller中引入以上两个包,如下所示: 3. 在controller中的ActionResult函数中按照如下最小结构进行调用: 1

MVC.Net:Razor指定模板

在MVC.Net开发中,我们通常会在_ViewStart.cshtml中指定一个默认的模板,在文件开头输入如下代码: @{ Layout = "~/Views/Shared/[自己定义的模板文件]"; } 那么,如何在每个单独的cshtml文件中使用特定的模板呢?也很简单,和_ViewStart.cshtml文件一样,在文件开头输入如下代码: @{ Layout = "~/Views/Shared/[自己定义的模板文件]"; } 那么,如果你不想要指定任何模板,想要

.net mvc RazorEngine 字符串razor参数替换

在.net中有一个比较好的字符串参数替换的方案RazorEngine推荐大家看看原网站,然后做个小联系然后你就懂啦 首先呢得下载一个吧, vs中tools-> Library Paging Manager->Manager Nuget 在然后呢Install-Package RazorEngine 等待搜索结束吧,然后下载下来两个dll RazorEngine.dll  没说的一定要引用到工程里面的 System.Web.Razor.dll 这个dll工程里面是引用了的  多以会提示替换,别犹

2014-07-29 浅谈MVC框架中Razor与ASPX视图引擎

今天是在吾索实习的第15天.随着准备工作的完善,我们小组将逐步开始手机端BBS的开发,而且我们将计划使用MVC框架进行该系统的开发.虽然我们对MVC框架并不是非常熟悉,或许这会降低我们开发该系统的效率,但是我们可以通过边学边做的方式来实现其开发的.这不仅便于我们日后对系统的管理与维护,而且还给我们带来一个学习的动力与实践的地方. 但我们在创建一个基于MVC框架的项目时,就遇到一些问题了.那就是MVC的视图引擎是有两种的,一种是Razor,会以cshtml后缀的文件作为视图文件:另一种是ASPX,