Mvc-model data Annotations (i)-System (DataAnnotations)

Source: Internet
Author: User

1. Basic Verification:
usingSystem.ComponentModel.DataAnnotations;//Field Display Name[Display (Name ="User name")]//data type (such as the type of generated text box)[DataType (Datatype.password)]//non-null validation[Required (errormessage ="*")]//character Length Verification[Stringlength ( -, errormessage ="the character length should be between 6-50", Minimumlength =6)]//Validation Scope[Range (Ten, -, errormessage ="The age should be between 10-120")]//regular-expression validation[RegularExpression (@"[A-za-z0-9._%+-][email protected][a-za-z0-9._]+\. [A-za-z] {2,4}", errormessage ="The mailbox format is not correct")]//Mailbox Format Validation[EmailAddress (errormessage ="The mailbox format is not correct")]

Where:errormessage inside is a custom error message , if you do not add this attribute, will display the system default prompt error message (the system hint is more blunt possible), generally add this attribute.

ErrorMessage allows the developer to use the {0} placeholder to display the display name of the field (that is, [display (name = "user name")]), and if there is no display attribute, the property name is displayed. such as:

" {0} cannot be empty!    "user name")] public string get  set; }    

If there are additional parameters in the validated attribute, then errormessage can display additional parameters directly with a placeholder, such as:

[Required][stringlength ( Please enter {2} to {1} bits {0}.  "6  " password ")] public string  getset;} // the system prompts: "Please enter a password of 6 to 100 bits". 

2. Additional Verification:

Additional validation is an additional two validation feature in SYSTEM.WEB.MVC, so you must add a using SYSTEM.WEB.MVC when you use it

1. Remote AuthenticationAlthough ASP. NET MVC3 provides many features that validate data directly in the model, many logical and complex validations are not validated in model, so the MVC3 framework provides this Remote authentication feature that allows developers to use C # in Controller        Code to verify the validity of the data. A very classic app when verifying that the user name is duplicated. This data verifies that there is no way to authenticate to the client unless all user names are sent to the client (obviously this is not possible). So the remote feature is only server-side verified. But it is validated in an asynchronous way, so there is a better user experience. Model:
        [Required]        [Display (name = "user name")]        [Remote ("Checkusername", "Account")]        public string UserName {get; set;}

Controller:

        Public Jsonresult Checkusername (string userName)        {            var result = UserName = = "admin";            return Json (result, jsonrequestbehavior.allowget);        }

Validation results:

Note: If you want post submission, you need to add httpmethod= "POST":

        [Display (name = "user name")]        [Remote ("Checkusername", "Account", httpmethod= "POST")]        public string UserName {get; set;}

The controller can also directly accept the POST request, of course, you add [HttpPost] is also possible:

        [HttpPost] public        jsonresult checkusername (string userName)        {            var result = userName! = "admin";            return Json (result, jsonrequestbehavior.allowget);        }

2, Compare (same authentication)

The Compare attribute is used to verify that the two data entered are identical. The most typical example is whether the password entered twice at the time of registration is the same.

Model:
        [Required]        [Stringlength (errormessage = "{0} must contain at least {2} characters. ", Minimumlength = 6)] [        DataType (Datatype.password)]        [Display (Name =" password ")] public        string Password {get; Set }          [DataType (Datatype.password)]        [Display (Name = "Confirm password")]        [Compare ("Password", errormessage = " The password and confirmation password do not match. ")] public        string ConfirmPassword {get; Set }

Validation results:

Above is the MVC framework of several common data annotations, in addition to the latter two types of MVC characteristics, the other features are in the System.ComponentModel.DataAnnotations.

Mvc-model data Annotations (i)-System (DataAnnotations)

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.