ASP.NET MVC5网站开发用户注册(四)(3)

@model Ninesky.Web.Areas.Member.Models.RegisterViewModel @{ ViewBag.Title = "注册"; Layout = "~/Views/Shared/_Layout.cshtml"; } @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> <h4>用户注册</h4> <hr /> @Html.ValidationSummary(true) <div> @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> </div> <div> @Html.LabelFor(model => model.DisplayName, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.DisplayName) @Html.ValidationMessageFor(model => model.DisplayName) </div> </div> <div> @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> </div> <div> @Html.LabelFor(model => model.ConfirmPassword, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.ConfirmPassword) @Html.ValidationMessageFor(model => model.ConfirmPassword) </div> </div> <div> @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) </div> </div> <div> @Html.LabelFor(model => model.VerificationCode, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.VerificationCode) <img title="点击刷新" src="https://www.jb51.net/@Url.Action("VerificationCode")" /> @Html.ValidationMessageFor(model => model.VerificationCode) </div> </div> <div> <input type="checkbox" checked="checked" required />我同意 <a href="#">《用户注册协议》</a> </div> <div> <div> <input type="submit" value="注册" /> </div> </div> </div> } <script type="text/javascript"> $("#verificationcode").click(function () { $("#verificationcode").attr("src", "https://www.jb51.net/@Url.Action("VerificationCode")?" + new Date()); }) </script> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

再在用户控制器中添加public ActionResult Register(RegisterViewModel register)用来处理用户提交的注册数据

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Register(RegisterViewModel register) { if (TempData["VerificationCode"] == null || TempData["VerificationCode"].ToString() != register.VerificationCode.ToUpper()) { ModelState.AddModelError("VerificationCode", "验证码不正确"); return View(register); } if(ModelState.IsValid) { if (userService.Exist(register.UserName)) ModelState.AddModelError("UserName", "用户名已存在"); else { User _user = new User() { UserName = register.UserName, //默认用户组代码写这里 DisplayName = register.DisplayName, Password = Security.Sha256(register.Password), //邮箱验证与邮箱唯一性问题 Email = register.Email, //用户状态问题 Status = 0, RegistrationTime = System.DateTime.Now }; _user = userService.Add(_user); if (_user.UserID > 0) { return Content("注册成功!"); //AuthenticationManager.SignIn(); } else { ModelState.AddModelError("", "注册失败!"); } } } return View(register); }

代码中很多根用户设置相关的内容先不考虑,等做到用户设置时在会后来修改。注册失败时返回视图并显示错误;成功时返回视图注册成功,等下次做用户登录时可以让用户注册完毕直接进行登录。看看效果。

ASP.NET MVC5网站开发用户注册(四)

点击注册,注册成功。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwxsx.html