MVC下的分页代码(MVC+Webservice)

首先声明,这是网上找的代码加上了一些自己的理解,为了让初学者少走一些弯路,我是利用三层架构WebApi传值+MVC实现的简单分页控件

下面是正文


Model

 1 //这个model类是为了存储总数和所有的数据,一起传到前台
 2 
 3 public class PagingModels<T> where T:class,new()
 4 {
 5 /// <summary>
 6 /// 数据
 7 /// </summary>
 8 public T tb { get; set; }
 9 /// <summary>
10 /// 总数
11 /// </summary>
12 public int Total { get; set; }
13 }

DAL

 1 /// <summary>
 2 /// 带存储过程并且分页的查询
 3 /// </summary>
 4 /// <param name="procName">存储过程的名称</param>
 5 /// <param name="Total">输出总数</param>
 6 /// <param name="sqlParameters">参数</param>
 7 /// <returns>返回Dataset</returns>
 8 public DataSet Query(string procName, ref int Total,SqlParameter[] sqlParameters)
 9 {
10 //定义一个dataset类型
11 DataSet dataSet = new DataSet();
12 //连接数据库
13 using (SqlConnection connection = new SqlConnection(con))
14 {
15 //打开数据库
16 connection.Open();
17 //封装执行的sql语句
18 using (SqlCommand command = new SqlCommand(procName, connection))
19 {
20 //将存储过程的输入参数放到comand中
21 command.Parameters.AddRange(sqlParameters);
22 //指定执行的是存储过程
23 command.CommandType = CommandType.StoredProcedure;
24 //使用适配器关联command
25 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
26 //适配器填充数据
27 sqlDataAdapter.Fill(dataSet);
28 //获取输出参数
29 Total=int.Parse(command.Parameters["@Total"].Value.ToString());
30 }
31 }
32 return dataSet;
33 }

BLL

 1 /// <summary>
 2 /// 返回分页的存储过程查询的结果
 3 /// </summary>
 4 /// <param name="Total">总数</param>
 5 /// <param name="pageSize">每页显示的个数</param>
 6 /// <param name="pageNumber">页数</param>
 7 /// <param name="where">条件</param>
 8 /// <param name="field">字段</param>
 9 /// <returns>DataTable</returns>
10 public DataTable GetPagingDept(ref int Total, int pageSize, int pageNumber, string where, string field)
11 {
12 SqlParameter[] sqlParameters = new SqlParameter[]
13 { 
14 new SqlParameter {ParameterName= "@table", Value= " /*这是需要的表名*/ ",SqlDbType= SqlDbType.NVarChar,Direction= ParameterDirection.Input, Size=200},
15 new SqlParameter {ParameterName= "@field", Value= field,SqlDbType= SqlDbType.NVarChar,Direction= ParameterDirection.Input, Size=2000 },
16 new SqlParameter {ParameterName= "@where", Value= where ,SqlDbType= SqlDbType.NVarChar,Direction= ParameterDirection.Input, Size=2000},
17 new SqlParameter {ParameterName= "@order", Value= " /*需要排序的Id*/ " ,SqlDbType= SqlDbType.NVarChar,Direction= ParameterDirection.Input, Size=200},
18 new SqlParameter {ParameterName= "@pageSize", Value=pageSize ,SqlDbType= SqlDbType.Int,Direction= ParameterDirection.Input},
19 new SqlParameter {ParameterName= "@pageNumber", Value=pageNumber ,SqlDbType= SqlDbType.Int,Direction= ParameterDirection.Input},
20 new SqlParameter {ParameterName= "@Total" ,SqlDbType= SqlDbType.Int,Direction= ParameterDirection.Output}
21 };
22 return helper.Query("[/*存储过程名称*/]", ref Total, sqlParameters).Tables[0];
23 }

WebApi控制器

 1 //一定记得配置跨域 
 2 [HttpGet]
 3         [Route("api/Show")]
 4         public IHttpActionResult ShowList(int pageNumber,int pageSize)
 5         {
 6 //定义变量
 7             int Total = 0;
 8            //接收从BLL传过来的值
 9             DataTable table = bll.ShowList(pageNumber, pageSize, “ 1=1 ”, " * ", ref Total);
10 //因为table是一个集合,而传值只能传一个,我们还需要获取总数,所以需要一个类来另外处理
11             PagingModel<DataTable> paging = new PagingModel<DataTable> { tb = table, Total = Total };
12 //返回值
13             return Ok(paging);
14         }

MVC视图页

 1     <input type="button" id="FirstNum" value="首页" />
 2     <input type="button" id="PrevNum" value="上一页" />
 3     <input type="button" id="NextNum" value="下一页" />
 4     <input type="button" id="EndNum" value="尾页" />

 7 <link href="Content/bootstrap.css" rel="stylesheet" />
 8 <script src="Content/jquery-3.3.1.min.js"></script>
 9 
10 <script>
11     //定义初始值
12     var pageSize = 3;//一页显示的个数
13     var pageNumber = 1;//页数
14     var Total = 0;//总数
15 //页面加载事件
16     $(function () {
      //调用方法
17 ShowList(pageNumber); 18 }) 19 20 function ShowList(pageNumber) { 21 22 $.ajax({ 23 url: "",//这是WebApi的链接,注意数据与数据之间用&连接 24 25 dataType: "json", 26 type: "get", 27 success: function (result) { 28 29 //获取到总行数 30 Total = result.Total; 31 } 32 }) 33 34 } 35 36 //计算总页数 37 function GetMath() { 38 var math = Total / pageSize; 39 return Math.ceil(math); 40 } 41 //首页 42 $("#FirstNum").click(function () { 43 pageNumber = 1; 44 ShowList(pageNumber,); 45 }) 46 //上一页 47 $("#PrevNum").click(function () { 48 if (pageNumber - 1 <= 0) { 49 alert("已经是第一页了"); 50 return; 51 } else { 52 pageNumber = pageNumber - 1; 53 } 54 55 ShowList(pageNumber,); 56 }) 57 //下一页 58 $("#NextNum").click(function () { 59 if (pageNumber >= GetMath()) { 60 alert("已经是最后一页了"); 61 return; 62 } else { 63 pageNumber = pageNumber + 1; 64 } 65 66 ShowList(pageNumber); 67 }) 68    //尾页 69 $("#EndNum").click(function () { 70 pageNumber = GetMath(); 71 ShowList(pageNumber); 72 }) 73 74 75 </script>

 

原文地址:https://www.cnblogs.com/lvjingchao/p/13065944.html