To register as a user example
First step: Add a View model
public class Viewregmodels { [Display (name = "user name")] [Required (allowemptystrings = false, ErrorMessage = ") User name cannot be empty ")] public string UserName {get; set;} [Display (Name = "password"), DataType (Datatype.password)] [Required (allowemptystrings = false, errormessage = "Password cannot be empty")] public string Password {get; set;} [Display (Name = "Confirm password"), DataType (Datatype.password)] [Required (allowemptystrings = false, errormessage = "Confirm password cannot be empty")] [Compare ("Password", errormessage = "Password and Confirm password are not the same")] public string Confrimpassword {get; set;} public string ReturnUrl {get; set;}}
Step Two: Add a controller
- Add HttpGet
[HttpGet] public iactionresult Reg () { return View ();}
- Add HttpPost
[Httppost]public async task<iactionresult> Reg (viewregmodels view) { if (!this. Isformvalid ()) { Modelstate.addmodelerror (string. Empty, this. Forminvalidreason ()); Return await Task.fromresult (view); } var result = resolve<iuserservice> (). Reg (view. UserName, view. Password); if (!result. Succeeded) { Modelstate.addmodelerror (string. Empty, result. ToString ()); Return await Task.fromresult (view); } String url = resolve<iautoconfigservice> (). Getdata<userloginregconfig> (). Afterregurl; if (URL. IsNullOrEmpty ()) { URL = "/index"; } return Redirect (URL);}
- Precautions
- You must use this. Isformvalid to verify all the fields
- Use this. Forminvalidreason Display specific error validation information for a field
- Modelstate.addmodelerror to display the error messages submitted by the form
- Use asynchronous commits whenever possible
Step Three: Add views view file
@model zkcloud.core.user.viewmodels.viewregmodels<div class= "Userlogin row" > <form method= "POST" class= " Form-horizontal form-row-seperated "asp-controller=" User "asp-action=" Reg "> <div class=" Form-group-error "&G T <div class= "Col-md-offset-2 col-md-10" > <div asp-validation-summary= "All" class= "Text-danger" >& lt;/div> </div> </div> <div class= "Form-group" > <label Asp-for= "UserName" class= "col-md-2 control-label" ></label> <div class= "col-md-10" > <input asp-for= "UserName" class= "Form-control"/> <span asp-validation-for= "UserName" Class= "Tex T-danger "></span> </div> </div> <div class=" Form-group "> &L T;label asp-for= "Password" class= "col-md-2 control-label" ></label> <div class= "col-md-10" > <inPut asp-for= "Password" class= "Form-control"/> <span asp-validation-for= "Password" class= "Text-danger "></span> </div> </div> <div class=" Form-group "> <label Asp-for= "Confrimpassword" class= "col-md-2 control-label" ></label> <div class= "col-md-10" > <input asp-for= "Confrimpassword" class= "Form-control"/> <span asp-validation-for= "Confri Mpassword "class=" Text-danger "></span> </div> </div> <div class=" Form-gro Up "> <div class=" col-md-offset-2 col-md-10 "> <button class=" btn Green Btn-big "> members Registration </button> </div> </div> </form></div>
Precautions:
-
- Use <div asp-validation-summary= "All" class= "Text-danger" ></div> to display all error verification information
- So the field must be added validation <span asp-validation-for= "Confrimpassword" class= "Text-danger" ></span>
- In order to reduce unnecessary workload, and communication costs. HTML styles that do not allow pure soundtracks
- All background personnel must have complete control of the HTML structure
Standard form submission Sample code