In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
MVC4 production site how to register users, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
One user
1.1 user Registration
First, add the user registration model class UserRegister to Models, inherit from User, new the password field in the class, and add duplicate password and CAPTCHA fields. Finished code
/ user registration model / public class UserRegister: User {/ password / [Display (Name= "password", Description= "6-20 characters.")] [Required (ErrorMessage = "×")] [StringLength [DataType (DataType.Password)] public new string Password {get; set;} / confirm password / / [Display (Name = "confirm password", Description = "enter password again")] [Compare ("Password", ErrorMessage = "×")] [DataType (DataType.Password)] public string RePassword {get; set;} / CAPTCHA / / [Display (Name = "CAPTCHA", Description = "Please enter the CAPTCHA in the picture")] [Required (ErrorMessage = "×")] [StringLength (6 Magi MinimumLengthful 6 Magi ErrorMessage = "x")] public string VerificationCode {get; set;}}
Open Controllers, right-click on public ActionResult Register () to add view, select strongly typed view, model class select UserRegister
After the addition is completed, go to the Register.cshtml editing view, delete the automatically generated content, and manually enter the desired code. The code is as follows:
@ model CMS.Models.UserRegister@ {ViewBag.Title = "user registration"; Layout = "~ / Views/Shared/_Layout.cshtml";}
@ using (Html.BeginForm ()) {@ Html.ValidationSummary (true) user Registration @ Html.LabelFor (model = > model.UserName): @ Html.EditorFor (model = > model.UserName) @ Html.ValidationMessageFor (model = > model.UserName) @ Html.DisplayDescriptionFor (model = > model.UserName) @ Html.LabelFor (model = > model.Gender): @ Html.RadioButton ("Gender", 0) male @ Html.RadioButton ("Gender", 1) female @ Html.RadioButton ("Gender", 2) True) secrecy @ Html.ValidationMessageFor (model = > model) @ Html.DisplayDescriptionFor (model = > model) @ Html.LabelFor (model = > model.Password): @ Html.PasswordFor (model = > model.Password) @ Html.ValidationMessageFor (model = > model.Password) @ Html.DisplayDescriptionFor (model = > model.Password) @ Html.LabelFor (model = > model.RePassword): @ Html.PasswordFor (model = > model.RePassword) @ Html.ValidationMessageFor (model = > model. RePassword) @ Html.DisplayDescriptionFor (model = > model.RePassword) @ Html.LabelFor (model = > model.SecurityQuestion): @ Html.EditorFor (model = > model.SecurityQuestion) @ Html.ValidationMessageFor (model = > model.SecurityQuestion) @ Html.DisplayDescriptionFor (model = > model.SecurityQuestion) @ Html.LabelFor (model = > model.SecurityAnswer): @ Html.EditorFor (model = > model.SecurityAnswer) @ Html.ValidationMessageFor (model = > model.SecurityAnswer) @ Html.DisplayDescriptionFor (model = > model .SecurityAnswer) @ Html.LabelFor (model = > model.Email): @ Html.EditorFor (model = > model.Email) @ Html.ValidationMessageFor (model = > model.Email) @ Html.DisplayDescriptionFor (model = > model.Email) CAPTCHA: @ Html.TextBoxFor (model = > model.VerificationCode) @ Html.ValidationMessageFor (model = > model.VerificationCode)
Change the registration terms: @ Html.CheckBox ("Agreement", new {@ class= "required"}) I have read and agreed to the registration terms} $("# trydifferent") .click (function () {$("# verificationcode"). Attr ("src", "/ User/VerificationCode?" + new Date ();}) @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}
Let's start writing the code for the registration process.
In Controllers, add a Register () Action of [HttpPost] mode under public ActionResult Register () {return View ();}, as follows:
[HttpPost] public ActionResult Register (UserRegister userReg) {if (Session ["VerificationCode"] = = null | | Session ["VerificationCode"] .ToString () = "") {Error _ e = new Error {Title = "CAPTCHA does not exist", Details = "when the user registers, the CAPTCHA on the server side is empty Or the verification code submitted to the server is empty ", Cause =" you have stayed on the registration page for too long when you registered and the page has timed out. You bypass client verification and submit data to the server ", Solution =" return to the registration page, refresh and re-register "} Return RedirectToAction ("Error", "Prompt", _ e);} else if (Session ["VerificationCode"]. ToString ()! = userReg.VerificationCode.ToUpper ()) {ModelState.AddModelError ("VerificationCode", "×"); return View ();} userRsy = new UserRepository (); if (userRsy.Exists (userReg.UserName)) {ModelState.AddModelError ("UserName", "user name already exists"); return View ();} User _ user = userReg _ user.Password = Common.Text.Sha256 (userReg.Password); _ user.RegTime = System.DateTime.Now; if (userRsy.Add (_ user)) {Notice _ n = new Notice {Title = "registered successfully", Details = "you have successfully registered and the user is:" + _ user.UserName + ", please remember your password!" , DwellTime = 5, Navigation = Url.Action ("Login", "User")}; return RedirectToAction ("Notice", "Prompt", _ n);} else {Error _ e = new Error {Title = "registration failed", Details = "an unknown error occurred while registering the user", Cause = "system error", Solution = "return to the registration page, enter correct information and re-register to contact the website administrator"} Return RedirectToAction ("Error", "Prompt", _ e);}}
OK, run it to see the effect.
After entering the data point registration. OK saw the page with successful registration.
Take a look at the corresponding records in the database.
The registration function is complete.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.