MVC4 Making Web Tutorials Chapter II practical skills of user registration 2.1_

Source: Internet
Author: User

Finally started to do the user section, first to register
one user  
1.1 user Registration  
First, add the user registration model class in models Userregister inherits from user, new password field in class, and add duplicate password and Authenticode field. The completed code  

<summary>
 ///User Registration Model
 ///</summary> public
 class Userregister:user
 {
 ///< summary>
 ///password
 ///</summary>
 [Display (name= "password", description= "6-20 characters. ")]
 [Required (errormessage =" x ")]
 [Stringlength (20,minimumlength=6,errormessage =" x ")]
 [DataType ( Datatype.password)] public
 new string Password {get; set;}
 <summary>
 ///Confirm password
 ///</summary>
 [Display (Name = "Confirm Password", Description = "Enter password again.") ")]
 [Compare (" Password ", errormessage =" x ")]
 [DataType (Datatype.password)] public
 string Repassword {get; set;}
 <summary>
 ///Verification Code
 ///</summary>
 [Display (Name = "Captcha", Description = "Enter the verification code in the picture.") ")]
 [Required (errormessage =" x ")]
 [Stringlength (6,minimumlength=6,errormessage =" x ")]
 public String Verificationcode {get; set;}
 }

Open controllers, right-click the public ActionResult Register () to add a view, select a strongly typed view, and model class selection Userregister

After the completion of the add to the register.cshtml edit view, delete the automatically generated content, manually enter the code you want to complete the code as follows:  

@model CMS.
 Models.userregister @{viewbag.title = "User Registration";
Layout = "~/views/shared/_layout.cshtml"; } <div class= "banner" >  </div> @using (html.beginform ()) {@Html. ValidationSummary (True) <div class= "form" > <dl> <dt> User registration </dt> <dd> < Div class= "label" > @Html. Labelfor (model => model. UserName):</div> <div class= "Ctrl" > @Html. Editorfor (model => model. UserName) @Html. Validationmessagefor (model => model. UserName) @Html. Displaydescriptionfor (model => model. UserName) </div> </dd> <dd> <div class= "label" > @Html. Labelfor (model => model. Gender):</div> <div class= "Ctrl" > @Html. RadioButton ("Gender", 0) male @Html. RadioButton ("Gender", 1) female @ Html.radiobutton ("Gender", 2, true) confidential @Html. VALIDATIONMESSAGEFOR (model => model) @Html. Displaydescriptionfor (mod El => model) </div> </dd> <dd> <div class= "label" > @Html. Labelfor (model => model. Password):</div> <div class= "Ctrl" > @Html. Passwordfor (model => model. Password) @Html. Validationmessagefor (model => model. Password) @Html. Displaydescriptionfor (model => model. Password) </div> </dd> <dd> <div class= "label" > @Html. Labelfor (model => model. Repassword):</div> <div class= "Ctrl" > @Html. Passwordfor (model => model. Repassword) @Html. Validationmessagefor (model => model. Repassword) @Html. Displaydescriptionfor (model => model. Repassword) </div> </dd> <dd> <div class= "label" > @Html. Labelfor (model => model. securityquestion):</div> <div class= "Ctrl" > @Html. Editorfor (model => model. securityquestion) @Html. Validationmessagefor (model => model. securityquestion) @Html. Displaydescriptionfor (model => model. securityquestion) </div> </dd> <dd> <div class= "label" > @Html labelfor (model => model. Securityanswer):</div> <div class= "Ctrl" > @Html. Editorfor (model => model. Securityanswer) @Html. Validationmessagefor (model => model. Securityanswer) @Html. Displaydescriptionfor (model => model. Securityanswer) </div> </dd> <dd> <div class= "label" > @Html. Labelfor (model => model. Email):</div> <div class= "Ctrl" > @Html. Editorfor (model => model. Email) @Html. Validationmessagefor (model => model. Email) @Html. Displaydescriptionfor (model => model.
   Email) </div> </dd> <dd> <div class= "label" > Authenticode:</div> <div class= "Ctrl" > @Html. Textboxfor (model => model. Verificationcode) @Html. Validationmessagefor (model => model. Verificationcode)  <a id= "Trydifferent" style= "Cursor:pointer" > Change a </a> </div> &LT;/DD&GT <dd> <div class= "label" > Registration terms:</div> <div class= "Ctrl" > @Html. CheckBox ("agreement", new {@cla  Ss= "Required"}) I have read and agreed to the terms of registration </div> </dd> <dd> <div class= "label" ></div> <div class= "Ctrl" > <input type= "Submit" value= "registered"/> </div> </dd> </dl> <div class= "cl Ear "></div> </div>} <script type=" Text/javascript "> $ (" #trydifferent "). Click (function () {$ (" #v Erificationcode "). attr (" src ","/user/verificationcode? ")
 +new Date ());

 }) </script> @section Scripts {@Scripts. Render ("~/bundles/jqueryval")}

Start the code that writes the registration process below. &NBSP
in controllers, in public ActionResult Register () {return View ();} Add a register () Action in the [HttpPost] way below, with the following code: &NBSP

[HttpPost] public actionresult Register (Userregister userreg) {if (session["verificationcode"] = = NULL | | session["Verificationcode"]. ToString () = = "") {Error _e = new Error {Title = "Authentication code does not exist", Details = "When the user registers, the server side of the authentication code is empty, or the authentication code submitted to the server is empty", cause = " ;li> the time you sign up for the registration page has timed out </li><li> you have bypassed the client validation to submit data to the server </li> ", Solution =" return <a href= ' "+
  Url.action ("register", "User") + "' > Registration </a> page, reload after registration"};
  Return redirecttoaction ("Error", "Prompt", _e); else if (session["Verificationcode").
  ToString ()!= userReg.VerificationCode.ToUpper ()) {Modelstate.addmodelerror ("Verificationcode", "X");
  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 Success",Details = "You have successfully registered, the user is:" + _user. UserName + ", please bear in mind your password!
  ", Dwelltime = 5, navigation = Url.action (" Login "," User ")};
  Return redirecttoaction ("Notice", "Prompt", _n); else {Error _e = new Error {Title = "registration Failed", Details = "Unknown error occurred during user registration", cause = "System error", Solution = "<li> return <a href= ' "+ url.action (" register "," User ") +" ' > Registration </a> page, after entering the correct information, re-register </li><li> contact webmaster </
  Li> "};
  Return redirecttoaction ("Error", "Prompt", _e);
 }
 }

OK, run it and see how it works.

Enter the data point registration. OK See the successful registration page

Look, there's a record in the database.

The registration function is complete.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.