Standard form submission Sample code

Source: Internet
Author: User

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
  1. Add HttpGet
    [HttpGet] public iactionresult Reg () {     return View ();}

  2. 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);}

  3. 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

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.