asp.net mvc 简易通用自定义Pager实现分页
2016-08-23 20:23
671 查看
asp.net mvc 自定义Pager实现分页
Intro
一个WEB应用程序中经常会用到数据分页,本文将实现一个简单通用的分页组件,包含一个PagerModel(用来保存页码信息),一个
HtmlHelper的
Pager扩展方法和一个
PagedListModel<T>分页数据模型。
pager效果图如下:
int rowsCount = 0; try { List<Models.BlockEntity> blockList = BusinessHelper.BlockEntityHelper.GetPagedList(search.PageIndex, search.PageSize, out rowsCount, whereLambda, b => b.BlockTime, false); PagerModel pager = new PagerModel(search.PageIndex, search.PageSize, rowsCount); PagedListModel<Models.BlockEntity> dataList = new PagedListModel<Models.BlockEntity>() { Pager = pager, Data = blockList }; return View(dataList); } catch (Exception ex) { logger.Error(ex); throw; }
Controller
int rowsCount = 0; try { List<Models.BlockEntity> blockList = new Business.BLLBlockEntity().GetPagedList(search.PageIndex, search.PageSize, out rowsCount, whereLambda, b => b.BlockTime, false);//记录日志 PagerModel pager = new PagerModel(search.PageIndex, search.PageSize, rowsCount);//定义Pager Model PagedListModel<Models.BlockEntity> dataList = new PagedListModel<Models.BlockEntity>() { Pager = pager, Data = blockList };//定义返回到 View 的 PagedListModel return View(dataList); } catch (Exception ex) { logger.Error(ex);//记录日志 throw; }
页面处理
首先在页面顶部声明 model 模型@model PagedListModel<Models.BlockEntity>
在页面上遍历数据
@foreach (Models.BlockEntity item in Model.Data)
在需要添加分页信息的地方使用 HtmlHelper 的 Pager 扩展方法
@Html.Pager(Model.Pager)
View代码示例
@model PagedListModel<Models.BlockEntity> <table class="table table-hover"> <thead> <tr> <th>黑名单类型</th> <th>黑名单内容</th> <th>拉入黑名单时间</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach (Models.BlockEntity item in Model.Data) { string className = "bg-success"; if (!item.IsActive) { className = "bg-danger"; } <tr class="@className"> <td> @item.BlockType.TypeName </td> <td> @item.BlockValue </td> <td> @item.BlockTime </td> <td> @if (item.IsActive) { <span>启用</span> } else { <span>禁用</span> } </td> <td> @if (item.IsActive) { <button type="button" class='btn btn-warning' onclick="UpdateStatus('@item.BlockId', '@item.BlockValue',-1,this)">禁用</button> } else { <button type="button" class='btn btn-warning' onclick="UpdateStatus('@item.BlockId','@item.BlockValue',1,this)">启用</button> } <button type="button" class="btn btn-danger" onclick="DeleteEntity('@item.BlockId','@item.BlockValue',this)">删除</button> </td> </tr> } </tbody> </table> @Html.Pager(Model.Pager)
相关文章推荐
- Asp.net Session 保存到MySql中
- asp.net微信开发第九篇----模板消息的使用
- Asp.net SignalR 实现服务端消息推送到Web端
- BouncyCastle使用教程
- C#利用NPOI导出Excel-ASP.NET MVC示例
- log4net使用教程
- asp.net 伪静态实现(UrlRewritingNet)
- ASP.NET MVC——URL路由
- ASP.NET 从数据库中导出数据
- ASP.NET 下拉列表绑定数据
- ASP.NET 使用Ajax
- ASP.NET 发起微信扫码支付
- 解读ASP.NET 5 & MVC6系列(11):Routing路由
- FineUI(专业版)v3.2.0 发布(ASP.NET UI控件库)!
- 关于asp.net 引用外部js文件或者json文件中文乱码的问题
- ASP.NET Core 中文文档 第三章 原理(15)请求功能
- ASP.NET MVC布局
- ASP.NET Core开发-后台任务利器Hangfire使用
- 在Raspberry上使用蓝牙4.0 BLE
- Raspberry 编译环境设置