欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC分页的实现方法

程序员文章站 2023-11-27 17:27:52
在这一篇文章中,我们将学习如何在mvc页面中实现分页的方法。分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页。在今天这篇文章中,我们学习如果在mvc页...

在这一篇文章中,我们将学习如何在mvc页面中实现分页的方法。分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页。在今天这篇文章中,我们学习如果在mvc页面中使用pagedlist.mvc包来实现分页功能。

1) 安装pagedlist.mvc

首先,我们需要安装分页组件包,在visual studio 2010中点击【项目】-【管理nuget程序包】,打开nuget包管理器窗体,在该窗体中,选择“联机”标签,然后搜索pagedlist,如下图所示。点击“安装”按钮安装pagedlist.mvc的最新版本(目前最新版本为4.5.0)。

ASP.NET MVC分页的实现方法

在把pagedlist.mvc安装完成之后,pagedlist包也被安装上了。如下图。

ASP.NET MVC分页的实现方法

图1:nuget包管理器中显示的pagedlist.mvc

2) 实现带分页功能的视图实体对象和控制器

把pagedlist.mvc安装完成之后,第一件事就是增加一个视图实体对象,用来放置一些查询属性与查询结果。在models目录下新增一个viewbook.cs文件,代码如下列所示:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using pagedlist;
 
namespace mvcapplication1.models
{
 public class viewbook
 {

   public ipagedlist<book> books { get; set; }
   public string search { get; set; }  

   public string category { get; set; }
   public string sortby { get; set; }  
 }
}

  我们现在需要修改bookcontroller类的searchindex方法,以便books作为pagedlist返回(使用topagedlist()方法完成)。为了使用pagedlist,我们还需要设置默认排序。为了使用pagedlist包,我们首先需要在该文件的顶部添加using pagedlist;代码,然后修改controllers\bookcontroller.cs文件为下列粗体显示的代码。

public actionresult searchindex(string category, string searchstring, string sortby,int? page)
 
 {

   var catelst = new list<string>();

   var cateqry = from d in db.books
       orderby d.category
       select d.category;
   catelst.addrange(cateqry.distinct());
   viewbag.category = new selectlist(catelst); 

   //排序选项
    var orderbylst = new dictionary<string, string>
  {
   { "价格从低到高", "price_lowest" },
   { "价格从高到低", "price_highest" }
  };

    viewbag.sortby = new selectlist(orderbylst, "value", "key");
   // [2017-2-14 end]
   var books = from m in db.books
      select m; 

   if (!string.isnullorempty(searchstring))
   {
    books = books.where(s => s.name.contains(searchstring));
   }

   // sort the results
   switch (sortby)
   {

    case "price_lowest":
     books = books.orderby(p => p.price);
     break;
    case "price_highest":
     books = books.orderbydescending(p => p.price);
     break;
    default:
     books = books.orderby(p => p.name);
     break;
   } 

   //分页
   const int pageitems = 5;
  int currentpage = (page ?? 1);
  ipagedlist<book> pagebooks = books.topagedlist(currentpage, pageitems);
  // [2017-2-14]
  viewbook vbook = new viewbook();
  vbook.books = pagebooks;
  vbook.category = category;
  vbook.sortby = sortby;
  vbook.search = searchstring;
   if (string.isnullorempty(category))
     vbook.books =pagebooks;
   else
   {
    vbook.books =pagebooks.where(x => x.category == category).topagedlist(currentpage, pageitems);
   }
   return view(vbook);
  }

  以上代码进行了以下几次发动,第一处改动是添加了一个int? page参数,它是一个可空整型,表示用户在书籍查询页面中选择的当前页码。当第一次加载书籍查询页面时,用户还没有选择任何页码,因此,这个参数可以为null。

  我们必须确保当前的分类也要保存在视图实体对象中,因此,我们添加了vbook.category = category;这行代码。

  代码books = books.orderby(p => p.name);用于对产品列表进行默认排序,这是因为pagedlist要求列表必须是一个有序列表。

  接着,我们使用代码const int pageitems = 5;来指定每页显示的数据数量。然后,我们声明了一个整型变量int currentpage = (page ?? 1);来保存当前页码,该变量的值是page参数的值,或者是1(当page变量为null时)。

  我们使用代码vbook.books = books.topagedlist(currentpage, pageitems);,对产品信息调用了topagedlist方法,并将当前页和每页显示的条目数传递给了topagedlist方法,然后将该方法的返回值赋值给了视图实体对象的books属性。

  我们使用代码viewbook.sortby = sortby;将sortby参数的值保存到视图实体对象的sortby属性中,以便我们从一页移动到另一页时,产品的排序保持不变。

3) 带分页功能的查询页面

   在视图实体对象和控制器中对实现分页功能的代码进行修改之后,现在,我们需要更新视图文件\views\products\searchindex.cshtml,在这个视图文件中显示一个分页控件,以便用户可以在各页之间移动。我们同时也添加了有多少条数据的指示信息。为了完成这些功能,我们在该文件中添加了一个using语句,一个书籍总数的指示信息以及在该页底部显示一个分页控件,具体代码如下面显示:

@model mvcapplication1.models.viewbook
 @using pagedlist.mvc

@{

 viewbag.title = "书籍查询";
}
 <link href="/content/pagedlist.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
<h2>书籍查询</h2>
  @using (html.beginform("searchindex","book",formmethod.get)){ 
   <p>书籍种类: @html.dropdownlist("category", "all") 
   书籍名称: @html.textbox("searchstring") 
    排序: @html.dropdownlist("sortby", "不排序")
   <input type="submit" value="查询" /> </p>
  }

<table>
 <tr>
  <th>
   @html.displaynamefor(model => model.books.first().category)
  </th>
  <th>
   @html.displaynamefor(model => model.books.first().name)

  </th>
  <th>
   @html.displaynamefor(model => model.books.first().numberofcopies)

  </th>
  <th>
   @html.displaynamefor(model => model.books.first().authorid)

  </th>
  <th>
   @html.displaynamefor(model => model.books.first().price)

  </th>
  <th>
   @html.displaynamefor(model => model.books.first().publishdate)

  </th>
  <th></th>

 </tr>
@foreach (var item in model.books) {

 <tr>
  <td>
   @html.displayfor(modelitem => item.category)

  </td>
  <td>
   @html.displayfor(modelitem => item.name)

  </td>
  <td>
   @html.displayfor(modelitem => item.numberofcopies)

  </td>
  <td>
   @html.displayfor(modelitem => item.authorid)

  </td>
  <td>
   @html.displayfor(modelitem => item.price)

  </td>
  <td>
   @html.displayfor(modelitem => item.publishdate)

  </td>
  <td>
   @html.actionlink("edit", "edit", new { id=item.bookid }) |

   @html.actionlink("details", "details", new { id=item.bookid }) |

   @html.actionlink("delete", "delete", new { id=item.bookid })

  </td>
 </tr>
}
</table>

<div>
  page @(model.books.pagecount < model.books.pagenumber ? 0 : model.books.pagenumber) of @model.books.pagecount

  @html.pagedlistpager(model.books, page => url.action("searchindex", new { category = model.category, 
search = model.search, sortby = model.sortby, page }))
 </div>

分页链接生成代码包裹在div标签内。其中第一行代码使用?:操作符的第一行代码决定是否有任何页码显示,它显示“page 0 of 0”或者“page x of y”,x表示当前页码,y表示总页数。

第二行代码使用来自于pagedlist.mvc命名空间的pagedlistpager辅助器。该辅助器接收一个产品列表参数,并为每个页面生成一个超链接。url.action用于生成一个含有当前页参数超链接目标。我们将一个匿名类型(含有当前分类、搜索条件、排序信息和分页)传递给该辅助器方法,以便每个页面的链接中都包含一个查询字符串,这个查询字符串包含有当前分类、搜索条件、排序信息和分页信息。这意味着,当从一个页面移动到另一个页面时,搜索条件、选择的分类和排序规则都被保存下来。如果没有这样做,书籍列表将会被重置为显示所有书籍信息。

在使用了上述代码后,按“价格从低到高”排序分页界面,如下图1。

ASP.NET MVC分页的实现方法

图1

    我们发现分页的数字部分,并不好看,原来我们缺少引用了css,在查询页面的标题下方添加如下代码。在上述代码中的蓝色字体。

<link href="/content/pagedlist.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />

再次点击“查询”按钮,然后对其结果按照“价格从低到高”进行排序,效果如下图2。

ASP.NET MVC分页的实现方法

图2:有搜索条件、排序和按分类过滤的分页效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。