注册--基于MVC的少儿兴趣班在线报名系统
Models:
public class RegisterModel { [Required] [StringLength(20, ErrorMessage = "{0}长度为7-20。", MinimumLength = 7)] [Display(Name = "用户名")] public string UserName { get; set; } [Required] [StringLength(100, ErrorMessage = "{0} 不能少于 {2} 个字符。", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "确认密码")] [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")] public string ConfirmPassword { get; set; } [Required] [Display(Name = "手机号码")] [RegularExpression(@"^1[0-9]\d{9}$", ErrorMessage = "手机号码格式不正确")] public string Phone { get; set; } }
controller:
[AllowAnonymous] public ActionResult Register() { return View(); } // // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // 尝试注册用户 try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); int UserId = WebSecurity.GetUserId(model.UserName); ChildrenInterestClassEntities db = new ChildrenInterestClassEntities(); var insertUserPhone = db.User.FirstOrDefault(u => u.UserId == UserId); insertUserPhone.Phone = model.Phone; db.SaveChanges(); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); }
views:
@model ChildrenInterestClass.Models.RegisterModel @{ ViewBag.Title = "注册"; } <link href="~/Content/Resgister.css" rel="stylesheet" type="text/css" /> <div class="div-resgister"> <div class="div-resgister-title"> <span class="span1">快速注册</span> </div> <div class="div-resgister-info"> @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "form1" })) { @AntiForgery.GetHtml() <div class="errorMessage"> @Html.ValidationSummary(true) </div> <div class="div1"> @Html.LabelFor(m => m.UserName, new { @class="label"}) @Html.TextBoxFor(m => m.UserName, new { @class="textBox"}) @Html.ValidationMessageFor(m => m.UserName) </div> <div class="div1"> @Html.LabelFor(m => m.Password, new { @class = "label" }) @Html.PasswordFor(m => m.Password, new { @class = "textBox" }) @Html.ValidationMessageFor(m => m.Password) </div> <div class="div1"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "label" }) @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "textBox" }) @Html.ValidationMessageFor(m => m.ConfirmPassword) </div> <div class="div1"> @Html.LabelFor(m => m.Phone, new { @class = "label" }) @Html.TextBoxFor(m => m.Phone, new { @class = "textBox" }) @Html.ValidationMessageFor(m => m.Phone) </div> <div class="div-ok-button"> <input type="submit" value="注册" id="Resgister"/> </div> } </div> </div>


浙公网安备 33010602011771号