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

asp.net mvc4 mysql制作简单分页组件(部分视图)

程序员文章站 2023-01-10 14:54:08
在开始做mysql分页功能组件前,便设定的是要有一定可复用性。先在项目里views文件夹下右键新建名为_paginationcomponent.cshtml,这里html及...

在开始做mysql分页功能组件前,便设定的是要有一定可复用性。先在项目里views文件夹下右键新建名为_paginationcomponent.cshtml,这里html及css我采用的bootstrap分页组件,这可以参考http://v3.bootcss.com/components/。

先将生成项目效果截图呈上:

asp.net mvc4 mysql制作简单分页组件(部分视图)

  这里有需要预先知道的,是mysql分页查询与mssql分页查询实现不同点在于,mysql支持limit语句,limit格式为 limit pageindex*pagesize,pagesize,pageindex--为页数,pagesize--为页面包含数据量。limit具体用法可查询mysql手册。然后需要预先定义好pagesize,pageindex,pagecount(分页总数)三个量。这里预设pagesize为30,即: int pagesize = 30。

  首先来实现获取pagecount:思路是,先将要获取的所有数据集从数据库中取出,根据pagesize = 30得出总页数,这里会遇到数据集数量为30的倍数与否问题,解决是:

 mysqlcommand comm_1 = new mysqlcommand(sqlsearch, connection);
   mysqldataadapter da_1 = new mysqldataadapter(sqlsearch, connection);
   da_1.selectcommand = comm_1; //初始化da.fill的命令
   dataset set_1 = new dataset();
   da_1.fill(set_1);
   datatable dt_1 = set_1.tables[0]; //使用datatable装所得多张表数据,并获取里面的第一张表  
   count = (double)(dt_1.rows.count) / 30;//分页总页数
   if (count > (int)(dt_1.rows.count) / 30)
   {
    pagecount = (dt_1.rows.count) / 30 + 1;
   }
   else if (count == (dt_1.rows.count) / 30)
   {
    pagecount = (dt_1.rows.count) / 30;
   }
   else if (count < (int)(dt_1.rows.count) / 30)
   {
    pagecount = 1;
   }

这里用到判断,大于30的pagecount均往上加1,小于30的pagecount为1。

接下来要实现的是用pageindex传页数获取对应的数据集,思路是利用limit语句特性: 

 public list<model> getdormitorybottlerecyclelistpagination(int pageindex, int pagesize)
  {
   get_connection();
   list<model> list = null;string sqlpaginatiosearch = "select * from table order by file_1 asc limit " + (pageindex - 1) * 30 + "," + pagesize + "";      //填充分页后的数据
   mysqlcommand comm_2 = new mysqlcommand(sqlpaginatiosearch, connection);
   mysqldataadapter da_2 = new mysqldataadapter(sqlpaginatiosearch, connection);
   da_2.selectcommand = comm_2; //初始化da.fill的命令
   dataset set_2 = new dataset();
   da_2.fill(set_2);
   datatable dt_2 = set_2.tables[0]; //使用datatable装所得多张表数据,并获取里面的第一张表     
   if (dt_2 != null && dt_2.rows.count > 0)
   {
    list = new list<model>();
    foreach (datarow row in dt_2.rows)
    {
     model entity = sqlhelper.createitem(row);
     list.add(entity);
    }
   }
   close_connection();
   return list;
  }

string sqlpaginatiosearch = "select * from table order by file_1 asc limit " + (pageindex - 1) * 30 + "," + pagesize + ""; //填充分页后的数据
这是核心sql语句,通过pageindex传入页面数,从(pageindex - 1) * 30处开始取pagesize量的数据。
在控制器的action中实现也是关键:

public actionresult dormitorybottlerecyclesort(int id = 1)
  {
   int pageindex = id;//传递分页数
   int pagesize = 30;
   int pagecount = 0;   
   list<bottlerecyclemodel> list_1;
   list_1 = pbsaccess.getdormitorybottlerecyclelistpagination(pageindex, pagesize, pagecount);//获取分页列表
   viewbag.listcount = list_1.count;
   bottlerecyclelist viewbottlerecyclelist = new bottlerecyclelist();
   viewbottlerecyclelist.bottlerecyclelist = new list<bottlerecyclemodel>();//要实例化对象,相当重要
        //这里是为显示分页数据的功能代码
   if (list_1 != null)
   {
    foreach (var item in list_1)
    {
     bottlerecyclemodel viewbottlerecycle = new bottlerecyclemodel();
     viewbottlerecycle.id = item.id;
     viewbottlerecycle.dormitorynumber = item.dormitorynumber;
     viewbottlerecycle.smallbottlenumber = item.smallbottlenumber;
     viewbottlerecycle.bigbottlenumber = item.bigbottlenumber;
     viewbottlerecycle.totalbottlenumber = item.totalbottlenumber;
     viewbottlerecycle.publishtime = item.publishtime;
     viewbottlerecyclelist.bottlerecyclelist.add(viewbottlerecycle);     
    }
    viewbag.dormitorybottlerecyclesort = viewbottlerecyclelist.bottlerecyclelist;    
   }
   else
   {
    viewbag.dormitorybottlerecyclesort = null;
   }
   
   viewbag.pagecount = pbsaccess.getdormitorybottlepaginationpagecount();
   viewbag.pageindex = pageindex;
   return view(viewbag);
  }

  这里使用viewbag进行传值,这里的getdormitorybottlepaginationpagecount()就是上面pagecount得出的方法。这是后台的方法。

  现在说一下_paginationcomponent.cshtml里该如何运用这些值。

@{
 string controllers = viewcontext.routedata.values["controller"].tostring();
 string actions = viewcontext.routedata.values["action"].tostring();
}
<li><a href="#">«</a></li>
@for (int i = 1; i < @viewbag.pagecount + 1; i++)
{
 <li><a href="/@controllers/@actions/@i">@i</a></li>
}

<li><a href="#">»</a></li>

  为了进行组件复用,采用viewcontext.routedata.values["controller"].tostring()方法,这样,在其他页面里引用组件时,可以轻易移植过去如:<a href="/@controllers/@actions/@i">@i</a>,当然,实用for循环是为了显示出更多的页数,诸如1、2、3、4等等,这里我的数据比较少,因此只显示一页,当然还有其他功能诸如省略过多页数为省略号和当前页禁用等等,需要js其他代码来实现,这里只为了实现一个简单的分页组件功能。

  这个页面里的代码并非全部分页的源码,这里只提供我个人解决时思路,如果有错误,还请指正,必定虚心求教。

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