终于开始做用户部分了,先做注册
一用户
1.1用户注册
首先在Models里添加用户注册模型类UserRegister 继承自User,在类中new一下密码字段,并添加重复密码和验证码字段。完成后的代码
/// <summary>
/// 用户注册模型
/// </summary>
public class UserRegister : User
{
/// <summary>
/// 密码
/// </summary>
[Display(Name="密码",Description="6-20个字符。")]
[Required(ErrorMessage = "×")]
[StringLength(20,MinimumLength=6,ErrorMessage = "×")]
[DataType(DataType.Password)]
public new string Password { get; set; }
/// <summary>
/// 确认密码
/// </summary>
[Display(Name = "确认密码", Description = "再次输入密码。")]
[Compare("Password", ErrorMessage = "×")]
[DataType(DataType.Password)]
public string RePassword { get; set; }
/// <summary>
/// 验证码
/// </summary>
[Display(Name = "验证码", Description = "请输入图片中的验证码。")]
[Required(ErrorMessage = "×")]
[StringLength(6,MinimumLength=6,ErrorMessage = "×")]
public string VerificationCode { get; set; }
}
打开Controllers,在public ActionResult Register()上点右键添加视图,选强类型视图,模型类选择UserRegister
添加完成后转到Register.cshtml编辑视图,删除掉自动生成的内容,手动输入想要的代码,完成后代码如下:
@model CMS.Models.UserRegister @{ ViewBag.Title = "用户注册"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div> <img src="https://www.jb51.net/~/Skins/Default/Images/banner.jpg" /> </div> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div> <dl> <dt>用户注册</dt> <dd> <div>@Html.LabelFor(model => model.UserName):</div> <div>@Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) @Html.DisplayDescriptionFor(model => model.UserName) </div> </dd> <dd> <div>@Html.LabelFor(model => model.Gender):</div> <div>@Html.RadioButton("Gender", 0) 男 @Html.RadioButton("Gender", 1) 女 @Html.RadioButton("Gender", 2, true) 保密 @Html.ValidationMessageFor(model => model) @Html.DisplayDescriptionFor(model => model) </div> </dd> <dd> <div>@Html.LabelFor(model => model.Password):</div> <div>@Html.PasswordFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) @Html.DisplayDescriptionFor(model => model.Password) </div> </dd> <dd> <div>@Html.LabelFor(model => model.RePassword):</div> <div>@Html.PasswordFor(model => model.RePassword) @Html.ValidationMessageFor(model => model.RePassword) @Html.DisplayDescriptionFor(model => model.RePassword) </div> </dd> <dd> <div>@Html.LabelFor(model => model.SecurityQuestion):</div> <div>@Html.EditorFor(model => model.SecurityQuestion) @Html.ValidationMessageFor(model => model.SecurityQuestion) @Html.DisplayDescriptionFor(model => model.SecurityQuestion) </div> </dd> <dd> <div>@Html.LabelFor(model => model.SecurityAnswer):</div> <div>@Html.EditorFor(model => model.SecurityAnswer) @Html.ValidationMessageFor(model => model.SecurityAnswer) @Html.DisplayDescriptionFor(model => model.SecurityAnswer) </div> </dd> <dd> <div>@Html.LabelFor(model => model.Email):</div> <div>@Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) @Html.DisplayDescriptionFor(model => model.Email) </div> </dd> <dd> <div>验证码:</div> <div> @Html.TextBoxFor(model => model.VerificationCode) @Html.ValidationMessageFor(model => model.VerificationCode) <img alt="" src="https://www.jb51.net/@Url.Action("VerificationCode", "User")" /> <a>换一张</a> </div> </dd> <dd> <div>注册条款:</div> <div> @Html.CheckBox("Agreement",new {@class="required"}) 我已阅读并同意注册条款 </div> </dd> <dd> <div></div> <div> <input type="submit" value="注册" /> </div> </dd> </dl> <div></div> </div> } <script type="text/javascript" > $("#trydifferent").click(function () { $("#verificationcode").attr("src", "/User/VerificationCode?"+new Date()); }) </script> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }