ASP. NET MVC uses dataannotations for model validation

Source: Internet
Author: User

By adding the dataannotations of data descriptions to the model classes, we can easily add validation capabilities to the application. DataAnnotations allows us to describe the validation rules that you want to apply on model properties, and ASP. NET MVC will use these dataannotations and then return the appropriate validation information to the user.

Common dataannotations include:

    1. Required must – Indicates that this property is a field that must provide content
    2. DisplayName Display Name – Defines the prompt name for form fields
    3. Stringlength String length – Defines the maximum length of a property of a string type
    4. Range Range – provides the maximum and minimum values for a property of a numeric type
    5. Bind binding – Lists the fields that are included and not included when the request parameters are bound to the model
    6. Scaffoldcolumn Bracket Column-a character that needs to be hidden when editing a form
    7. Compare comparison-See code for comparison with the established field values
usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;namespaceskuordermvc.models{ Public classChangePassword {[Required] [Display (Name="Email")] [EmailAddress] Public stringEmail {Get;Set; }        [Required]        [DataType (Datatype.password)] [Stringlength ( +, errormessage ="The {0} must is at least {2} characters long.", Minimumlength =6)] [Display (Name="Password")]         Public stringPassword {Get;Set; }        [DataType (Datatype.password)] [Display (Name="Confirm Password")] [Compare ("Password", errormessage ="The password and confirmation password do not match.")]         Public stringConfirmPassword {Get;Set; } }}

Of course, there are other attributes, such as regular expressions, which can be used to validate the data input format without the help of the code.

ASP. NET MVC uses dataannotations for model validation

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.