ASP.NET MVC Preview 5 演示Demo #5 实现MikePager数据分页和GridView控件
MVC演示Demo系列: http://mike108mvp.cnblogs.com
Demo #1 实现CRUD增删查改:http://www.cnblogs.com/mike108mvp/archive/2008/08/31/1280689.html
Demo #2 实现Membership角色权限控制:http://www.cnblogs.com/mike108mvp/archive/2008/09/03/1283432.html
Demo #3 实现MVC批量上传文件:http://www.cnblogs.com/mike108mvp/archive/2008/09/04/1284087.html
Demo #4 实现RadioButtonList与DropDownList应用:http://www.cnblogs.com/mike108mvp/archive/2008/09/15/1291153.html
ASP.NET MVC QQ交流群1:1215279 (人已满,但不定期清理)
ASP.NET MVC QQ交流群2:1214648
ASP.NET MVC QQ交流群3:47788243 (可容纳500人)
欢迎对 ASP.NET MVC 感兴趣的朋友加入以上的QQ群
This Mvc Demo #5 demonstrates how to use customized Html.GridView() and Html.MikePager() for data listing and data paging. Hope this helps, enjoy it.
Download the Code:https://files.cnblogs.com/mike108mvp/Mike108Demo05.rar
这个演示项目实现了在ASP.NET MVC中使用自定义的Html.GridView()控件和Html.MikePager()数据分页控件。【ok】
开发环境:Visual Studio 2008 SP1 + ASP.NET MVC Preview 5 + Linq To Sql + SQL Server 2005
ASP.NET MVC Preview 5 下载地址: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16775
【Step By Step 操作步骤】
1、SQL Server 2005 执行下面的Sql查询脚本。该Sql脚本将生成一个新的mike108mvp数据库,里面包含两个表User、UserType。
注意:这个数据库脚本与Demo #4 的脚本一样,如果你已经执行过Demo #4的数据库脚本,则可以跳过这一步。【ok】
数据库sql脚本
/**//****** 先删除再重新生成数据库 ******/
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'mike108mvp')
DROP DATABASE [mike108mvp]
CREATE DATABASE [mike108mvp]
GO
/**//****** 生成表结构 ******/
USE [mike108mvp]
GO
/**//****** 对象: Table [dbo].[UserType] 脚本日期: 09/15/2008 15:53:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserType](
[TypeId] [int] NOT NULL,
[TypeName] [nvarchar](50) NULL,
CONSTRAINT [PK_UserType] PRIMARY KEY CLUSTERED
(
[TypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
USE [mike108mvp]
GO
/**//****** 对象: Table [dbo].[User] 脚本日期: 09/15/2008 15:54:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NULL,
[TypeId] [int] NOT NULL,
[Sex] [bit] NOT NULL,
[Age] [int] NULL,
[Career] [nvarchar](50) NULL,
[FileName] [nvarchar](200) NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserId] 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
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'UserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'UserName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'分类' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'TypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'性别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Sex'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年龄' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Age'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'职业' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Career'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'附件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'FileName'
GO
ALTER TABLE [dbo].[User] WITH CHECK ADD CONSTRAINT [FK_User_UserType] FOREIGN KEY([TypeId])
REFERENCES [dbo].[UserType] ([TypeId])
GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_User_UserType]
/**//****** 脚本结束 ******/
2、下载本演示源码:https://files.cnblogs.com/mike108mvp/Mike108Demo05.rar
Visual Studio 2008 双击打开下载的演示源码中的 Mike108Demo.sln 解决方案文件,按F5运行。【ok】
3、Views/Users/List.aspx 数据分页和GridView控件效果图。【ok】
4、UsersController.cs 代码:【ok】
public class UsersController : Controller
{
Mike108mvpDataContext db = new Mike108mvpDataContext();
Demo #1 CRUD操作#region Demo #1 CRUD操作
public ActionResult UserAdd()
{
return View(@"Add");
}
public ActionResult UserCreate()
{
if (Request.HttpMethod != "POST")
{ return View(@"Add"); }
User model = new User();
UpdateModel(model, Request.Form.AllKeys);
db.Users.InsertOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserEdit(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Edit", model);
}
public ActionResult UserUpdate(int userId)
{
if (Request.HttpMethod != "POST")
{ return RedirectToAction("UserEdit", new { userId = userId }); }
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
UpdateModel(model, Request.Form.AllKeys);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserDetail(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Detail", model);
}
public ActionResult UserDelete(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
db.Users.DeleteOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserList1()
{
if (db.Users.Count() == 0 || db.UserTypes.Count() == 0)
{
return RedirectToAction("DataReset");
}
ViewData["UserList"] = db.Users.ToList();
return View(@"List");
}
#endregion
Demo #1 数据重置#region Demo #1 数据重置
public ActionResult DataReset()
{
//批量添加 用户分类
if (db.UserTypes.Count() == 0)
{
List<UserType> userTypelist = new List<UserType>
{
new UserType { TypeId=1, TypeName = "总统" },
new UserType { TypeId=2, TypeName = "独裁者" },
new UserType { TypeId=3, TypeName = "S.H.E" },
new UserType { TypeId=4, TypeName = "小虎队" },
new UserType { TypeId=5, TypeName = "NBA" },
new UserType { TypeId=6, TypeName = "科学家" }
};
db.UserTypes.InsertAllOnSubmit(userTypelist);
db.SubmitChanges();
}
//批量删除 用户
List<User> oldList = db.Users.ToList();
db.Users.DeleteAllOnSubmit(oldList);
//批量添加 用户
List<User> userlist = new List<User>
{
new User { UserName = "孙中山", TypeId=1, Sex=true, Age = 53, Career = "中国总统" },
new User { UserName = "蒋中正", TypeId=1, Sex=true, Age = 36, Career = "中国总统" },
new User { UserName = "小布什", TypeId=1, Sex=true, Age = 61, Career = "美国总统" },
new User { UserName = "列宁", TypeId=2, Sex=true, Age = 49, Career = "邪恶苏联" },
new User { UserName = "斯大林", TypeId=2, Sex=true, Age = 49, Career = "邪恶苏联" },
new User { UserName = "Selina", TypeId=3, Sex=false, Age = 26, Career = "女艺人" },
new User { UserName = "Hebe", TypeId=3, Sex=false, Age = 25, Career = "女艺人" },
new User { UserName = "Ella", TypeId=3, Sex=false, Age = 26, Career = "女艺人" },
new User { UserName = "吴奇隆", TypeId=4, Sex=true, Age = 34, Career = "男艺人" },
new User { UserName = "苏有朋", TypeId=4, Sex=true, Age = 33, Career = "男艺人" },
new User { UserName = "陈志朋", TypeId=4, Sex=true, Age = 35, Career = "男艺人" },
new User { UserName = "乔丹", TypeId=5, Sex=true, Age = 39, Career = "球星" },
new User { UserName = "皮蓬", TypeId=5, Sex=true, Age = 38, Career = "球星" },
new User { UserName = "罗德曼", TypeId=5, Sex=true, Age = 37, Career = "球星" },
new User { UserName = "姚明", TypeId=5, Sex=true, Age = 27, Career = "球星" },
new User { UserName = "易建联", TypeId=5, Sex=true, Age = 20, Career = "球星" },
new User { UserName = "科比", TypeId=5, Sex=true, Age = 29, Career = "球星" },
new User { UserName = "麦蒂", TypeId=5, Sex=true, Age = 33, Career = "球星" },
new User { UserName = "艾弗森", TypeId=5, Sex=true, Age = 29, Career = "球星" },
new User { UserName = "詹姆斯", TypeId=5, Sex=true, Age = 29, Career = "球星" },
new User { UserName = "加内特", TypeId=5, Sex=true, Age = 32, Career = "球星" },
new User { UserName = "比尔盖茨", TypeId=6, Sex=true, Age = 62, Career = "微软老大" },
new User { UserName = "Scott Guthrie", TypeId=6, Sex=true, Age = 33, Career = "ASP.NET Leader" }
};
db.Users.InsertAllOnSubmit(userlist);
db.SubmitChanges();
return RedirectToAction("UserList");
}
#endregion
Demo #2 微软Membership权限#region Demo #2 微软Membership权限
[Authorize] //登陆后才能查看
public ActionResult UserMembership()
{
User model = db.Users.FirstOrDefault(e => e.UserName == "孙中山");
return View(@"Detail", model);
}
#endregion
Demo #3 实现MVC批量上传文件#region Demo #3 实现MVC批量上传文件
public ActionResult Upload()
{
return View(@"Upload");
}
public ActionResult UploadFiles()
{
var r = new List<MikeUploadFile>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\", Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
r.Add(new MikeUploadFile()
{
Name = savedFileName,
Length = hpf.ContentLength
});
}
return View(@"Upload", r);
}
#endregion
Demo #4 实现RadioButtonList和DropDownList应用#region Demo #4 实现RadioButtonList和DropDownList应用
// Demo #4 没有加入新的Controller代码
#endregion
Demo #5 实现MikePager数据分页和GridView控件#region Demo #5 实现MikePager数据分页和GridView控件
public ActionResult UserList(int? pageIndex, int? pageSize)
{
if (db.Users.Count() == 0 || db.UserTypes.Count() == 0)
{
return RedirectToAction("DataReset");
}
int pIndex = pageIndex ?? 1;
int pSize = pageSize ?? 10;
GridViewData<User> gridViewData = new GridViewData<User>();
gridViewData.PagedList = db.Users.OrderBy(e=>e.UserId).ToPagedList<User>(pIndex, pSize);
return View(@"List", gridViewData);
}
#endregion
}
5、Views/Users/List.aspx 加入下面的代码即可使用MikePager数据分页控件:【ok】
<div class="mike_mvc_pager" >
<%=Html.MikePager<User>(ViewData.Model.PagedList)%>
</div>
6、数据分页代码请看下载的Demo源码Helpers目录下的几个文件。【ok】
Download the Code:https://files.cnblogs.com/mike108mvp/Mike108Demo05.rar
MVC演示Demo系列: http://mike108mvp.cnblogs.com
Demo #1 实现CRUD增删查改:http://www.cnblogs.com/mike108mvp/archive/2008/08/31/1280689.html
Demo #2 实现Membership角色权限控制:http://www.cnblogs.com/mike108mvp/archive/2008/09/03/1283432.html
Demo #3 实现MVC批量上传文件:http://www.cnblogs.com/mike108mvp/archive/2008/09/04/1284087.html
Demo #4 实现RadioButtonList与DropDownList应用:http://www.cnblogs.com/mike108mvp/archive/2008/09/15/1291153.html