ASP.NET Mvc Preview 5 演示Demo #4 实现RadioButtonList与DropDownList应用
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
ASP.NET MVC QQ交流群:1215279 欢迎对 ASP.NET MVC 感兴趣的朋友加入
这个演示项目实现了在ASP.NET MVC 中用RadioButtonList编辑User性别,用DropDownList下拉列表选择UserType用户类别。【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 #3的脚本不同。无论你是否执行过Demo #3的脚本,都需要重新执行这个Demo #4的数据库脚本。【ok】
数据库脚本
/**//****** 先删除再重新生成数据库 ******/
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、下载本演示源码:http://files.cnblogs.com/mike108mvp/Mike108Demo04.rar
Visual Studio 2008 双击打开下载的演示源码中的 Mike108Demo.sln 解决方案文件,按F5运行。【ok】
3、Models/Status.cs输入:【ok】
Code
public class Status
{
public string Text { get; set; }
public object Value { get; set; }
/**//// <summary>
/// 用户性别
/// </summary>
public static List<Status> SexList
{
get
{
List<Status> list = new List<Status>()
{
new Status { Value = true, Text = "男" },
new Status { Value = false, Text = "女" }
};
return list;
}
}
}
4、Users/Add.aspx 输入以下代码。【ok】
Code
public partial class Add : ViewPage
{
Mike108mvpDataContext db = new Mike108mvpDataContext();
/**//// <summary>
/// 用户分类RadioButtonList
/// </summary>
protected string UserTypeRadioButtonList
{
get
{
List<UserType> list = db.UserTypes.ToList();
string[] rs = Html.RadioButtonList("TypeId", list, "TypeName", "TypeId");
StringBuilder sb = new StringBuilder();
foreach (string r in rs)
{
sb.Append(r);
}
return sb.ToString();
}
}
/**//// <summary>
/// 用户分类DropDownList
/// </summary>
protected string UserTypeDropDownList
{
get
{
List<UserType> list = db.UserTypes.ToList();
//string str = Html.DropDownList(null,"TypeId", new SelectList(list,"TypeId","TypeName") );
string str = Html.DropDownList("请选择", "TypeId", new SelectList(list, "TypeId", "TypeName"));
return str;
}
}
/**//// <summary>
/// 性别RadioButtonList
/// </summary>
protected string SexList
{
get
{
List<Status> list = Status.SexList;
string[] rs = Html.RadioButtonList("Sex", list, "Text", "Value");
StringBuilder sb = new StringBuilder();
foreach (string r in rs)
{
sb.Append(r);
}
return sb.ToString();
}
}
}
5、Users/Edit.aspx 输入类似代码。【ok】
6、如果你想实现新用户注册、登陆、修改密码功能,请点击下面的Demo #2(实现Membership角色权限控制)帖子链接。【ok】
下载本演示源码:http://files.cnblogs.com/mike108mvp/Mike108Demo04.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