共计 2667 个字符,预计需要花费 7 分钟才能阅读完成。
导读 | 这篇文章主要为大家详细介绍了 MVC 使用 MvcPager 实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 |
本文实例为大家分享了 MVC 使用 MvcPager 实现分页效果的具体代码,供大家参考,具体内容如下
一、数据库表
USE [StudentDB]
GO
/****** Object: Table [dbo].[UserInfo] Script Date: 07/27/2018 13:59:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserInfo]([customerID] [int] IDENTITY(1,1) NOT NULL,
[customerName] [varchar](50) NOT NULL,
[PID] [varchar](50) NOT NULL,
[telephone] [varchar](50) NOT NULL,
[address] [varchar](20) NULL,
PRIMARY KEY CLUSTERED
([customerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ_PID] UNIQUE NONCLUSTERED
([PID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[UserInfo] WITH CHECK ADD CONSTRAINT [CK_PID] CHECK ((len([PID])=(15) OR len([PID])=(18)))
GO
ALTER TABLE [dbo].[UserInfo] CHECK CONSTRAINT [CK_PID]
GO
ALTER TABLE [dbo].[UserInfo] WITH CHECK ADD CONSTRAINT [CK_telephone] CHECK ((len([telephone])=(11)))
GO
ALTER TABLE [dbo].[UserInfo] CHECK CONSTRAINT [CK_telephone]
GO
二、建立 Linq
三、在 Model 创建 UserInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web.Models
{
public class UserInfo
{
private int customerID;
public int CustomerID
{get { return customerID;}
set {customerID = value;}
}
private string customerName;
public string CustomerName
{get { return customerName;}
set {customerName = value;}
}
private string pid;
public string Pid
{get { return pid;}
set {pid = value;}
}
private string telephone;
public string Telephone
{get { return telephone;}
set {telephone = value;}
}
private string address;
public string Address
{get { return address;}
set {address = value;}
}
}
}
四、在 Controllers 创建 Home 控制器
添加 MvcPager.dll,并引用 MvcPager 的命名空间 Webdiyer.WebControls.Mvc。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using Webdiyer.WebControls.Mvc;
namespace Web.Controllers
{
public class HomeController : Controller
{
//
// GET: /Page/
// 默认分页
private const int defaultPageSize = 5;
//
public ActionResult Index(int? id)
{using (DBDataContext db = new DBDataContext())
{IQueryable p = from c in db.UserInfo
select new UserInfo {CustomerID = c.customerID, CustomerName = c.customerName, Telephone = c.telephone, Pid = c.PID, Address = c.address};
PagedList m = p.ToPagedList(id ?? 1, defaultPageSize);
return View(m);
}
}
}
}
五、添加视图 Index
fo>>" %>
Index
编号
姓名
身份证号
电话号码
地址
new AjaxOptions() { UpdateTargetId = "divpages"})%>--%>
{0}",
ShowPageIndexBox = true,
NumericPagerItemWrapperFormatString = "{0}",
PageIndexBoxType = PageIndexBoxType.DropDownList,
ShowGoButton = false,PageIndexBoxWrapperFormatString="转到 {0}",SeparatorHtml = "" })%>
以上就是本文的全部内容,希望对大家的学习有所帮助。
正文完
星哥玩云-微信公众号