数据库脚本:
- USE [SBDB]
- GO
- /****** Object: Table [dbo].[Test] scrip{过滤}t Date: 07/29/2016 11:48:16 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[Test](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NOT NULL,
- [Age] [int] NOT NULL,
- [Sex] [nvarchar](50) NOT NULL,
- [Hobby] [nvarchar](50) NOT NULL,
- [Status] [tinyint] NOT NULL
- ) ON [PRIMARY]
- GO
- SET IDENTITY_INSERT [dbo].[Test] ON
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (1, N'张山', 18, N'男', N'打球', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (2, N'李四', 52, N'男', N'英雄联盟', 2)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (4, N'王五', 22, N'男', N'游泳', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (5, N'小红', 25, N'女', N'听歌', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (7, N'实打实', 55, N'解决', N'都市风水师', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (8, N'阿斯顿撒', 88, N'戴菲菲', N'151', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (9, N'实打实的', 10, N'啥都', N'14144', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (10, N'是多少啊', 55, N'到底', N'幅度高达', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (11, N'吊死扶伤', 11, N'1155', N'144', 0)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (12, N'十大', 22, N'撒旦', N'的发热', 0)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (13, N'阿三倒萨', 88, N'1111', N'222', 1)
- INSERT [dbo].[Test] ([ID], [Name], [Age], [Sex], [Hobby], [Status]) VALUES (14, N'那你', 20, N'1541', N'152', 1)
- SET IDENTITY_INSERT [dbo].[Test] OFF
复制代码
控制器:
- public ActionResult Test(int? id)
- {
- int size = 10;
- int _page= id ?? 1;
- SB.CMS.Model.SB_DBDataContext db = new Model.SB_DBDataContext();
- int count = db.Test.Count();
- int page = count % size == 0 ? count / size : (count / size) + 1; //总页数
- dynamic obj = new System.Dynamic.ExpandoObject();
- obj.Count = page;
- obj.Page = _page;
- ViewBag.Page = obj;
- ViewBag.Data = db.Test.Skip((_page - 1) * size).Take(page != _page ? size : (count % size == 0 ? size : count % size)).ToList();
- return View();
- }
复制代码
前台试图页面:
- @{
- Layout = null;
- List<SB.CMS.Model.Test> _list = ViewBag.Data as List<SB.CMS.Model.Test>;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Test</title>
- <!-- 新 Bootstrap 核心 CSS 文件 -->
- <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
- <!-- 可选的Bootstrap主题文件(一般不用引入) -->
- <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
- <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
- <scrip{过滤}t src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></scrip{过滤}t>
- <!-- 最新的 Bootstrap 核心 Javascrip{过滤}t 文件 -->
- <scrip{过滤}t src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></scrip{过滤}t>
- <!-- 分页 -->
- <scrip{过滤}t src="~/scrip{过滤}ts/bootstrap-paginator.js"></scrip{过滤}t>
- </head>
- <body>
- <div class="container">
- <div class="row">
- @{
- var data = new ViewDataDictionary();
- data.Add("page", ViewBag.Page);
- Html.RenderPartial("/Views/Templet/_Page.cshtml", _list, data);
- }
- </div>
- </div>
- </body>
- </html>
复制代码
分页视图:
- @model List<SB.CMS.Model.Test>
- @{
- dynamic page = (dynamic)Html.ViewData["page"];
- }
- <table class="table">
- <thead>
- <tr>
- <th>编号</th>
- <th>姓名</th>
- <th>年龄</th>
- <th>性别</th>
- <th>爱好</th>
- <th>状态</th>
- </tr>
- </thead>
- <tbody>
- @{
- foreach (var item in Model)
- {
- <tr>
- <th>@item.ID</th>
- <td>@item.Name</td>
- <td>@item.Age</td>
- <td>@item.Sex</td>
- <td>@item.Hobby</td>
- <td>@item.Status</td>
- </tr>
- }
- }
- </tbody>
- </table>
- <ul class="pagination" id="pageUl"></ul>
- <scrip{过滤}t>
- $(function () {
- var element = $('#pageUl');//对应下面ul的ID
- var count=@page.Count;
- var page=@page.Page;
- var options = {
- itemTexts: function (type, page, current) {
- switch (type) {
- case "first":
- return "首页";
- case "prev":
- return "上一页";
- case "next":
- return "下一页";
- case "last":
- return "末页";
- case "page":
- return page;
- }
- },
- //点击事件
- onPageClicked: function (event, originalEvent, type, page) {
- //alert(page);
- document.locatio{过滤}n.href="/Home/Test/"+page;
- },
- currentPage: @page.Page,//当前页面
- numberOfPages: 5,//一页显示几个按钮(在ul里面生成5个li)
- totalPages: @page.Count, //总页数
- bootstrapMajorVersion: 3
- };
- element.bootstrapPaginator(options);
- });
- </scrip{过滤}t>
复制代码
|