MVC分页

http://www.webdiyer.com/mvcpager/downloads/   下载  MvcPagerDemoNet4.0.zip

新建MVC 项目引用 下载下来的  MvcPager.dll

然后在要分页的Controllers   index  方法中 修改成 

 public ActionResult Index(int? page = 1)
        {
            var ggxxlook = db.GGXX.OrderByDescending(s => s.id);
            return View(ggxxlook.ToPagedList(page ?? 1, 5));
        }

  视图中 index  加入

@model  PagedList<MvcApplication3.Models.相关的类>
@using Webdiyer.WebControls.Mvc;

分页条写在视图最下面

<p>共有 @Model.TotalItemCount 条记录     @Model.CurrentPageIndex/@Model.TotalPageCount</p>

@Html.Pager(Model, new PagerOptions { PageIndexParameterName = "page", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.TextBox, PageIndexBoxWrapperFormatString = "请输入页数{0}" }, "Default", new { sortOrder = ViewBag.DateSortParm, searchString = ViewBag.Filter })

  @section Scripts{@{Html.RegisterMvcPagerScriptResource();}}

  

config 中加入  

   <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
        <add namespace="Webdiyer.WebControls.Mvc"/> <!--添加这个 -->
      </namespaces>
    </pages>

  

要想能传参数改成下面的样子

    public ActionResult Index(string sortOrder, string searchString, int? page = 1)
        {
            ViewBag.NameSortParm = string.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
            var dd = db.GGXX;
            //.OrderByDescending(s => s.id)
            var students = dd.Where(c => true);
            ViewBag.Filter = searchString;
            if (!string.IsNullOrEmpty(searchString))
            {
        //搜索的内容 students = students.Where(s => s.CreatePeople.Contains(searchString)); }
        //排序的关键字 switch (sortOrder) { case "Name desc": students = students.OrderByDescending(s => s.CreatePeople); break; case "Date": students = students.OrderBy(s => s.CreatePeople); break; case "Date desc": students = students.OrderByDescending(s => s.CreatePeople); break; default: students = students.OrderBy(s => s.CreatePeople); break; } //students = students.OrderByDescending(s => s.id); return View(students.ToPagedList(page ?? 1, 5)); }

  视图中加入 

@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")  
        <input type="submit" value="Search" />
    </p>
}

  

原文地址:https://www.cnblogs.com/crazyair/p/3976718.html