Use annotation to validate data in the. NET MVC model __.net

Source: Internet
Author: User

The available properties are as follows:

Data Annotation Validator Attributes DataType

Specify the datatype of a property DisplayName

Specify the display name for a. DisplayFormat

Specify the display format for a property like different format for Date Proerty. Required

Specify a property as required. reqularexpression

Validate the value of a property by specified regular expression pattern. Range

Validate the value of a property with in a specified range of values. Stringlength

Specify min and max length for a string. MaxLength

Specify max length for a string. Bind

Specify fields to include or exclude if adding parameter or form values to model properties. Scaffoldcolumn

Specify fields for hiding from editor forms.


The specific usage is as follows:

Using System.ComponentModel;
Using System.ComponentModel.DataAnnotations;
Using SYSTEM.WEB.MVC; namespace Employee.models {[Bind (Exclude = "EmpId")] public class Employee {[Scaffoldcolumn (false)] public int Empi
 
 d {get; set;}
 [DisplayName ("Employee Name")]
 [Required (errormessage = "Employee Name is Required")] 
 
 [Stringlength (100,minimumlength=3)] public String empname {get; set;} 
 [Required (errormessage = "Employee address is Required")] 
 
 [Stringlength] public string Address {get; set;} 
 [Required (errormessage = "Salary is Required")] 
 
 [Range (3000, 10000000,errormessage = "Salary must is between 3000 and 10000000")] public int salary{get; set;} 
 [Required (errormessage = "Please enter your e-mail address")]
 [DataType (Datatype.emailaddress)]
 [Display (Name = "Email address")]
 [MaxLength (50)] [RegularExpression (@] [a-z0-9._%+-]+@[a-z0-9.-]+\.[
 a-z]{2,4} ", errormessage =" Please enter correct e-mail ")] public string Email {get; set;} }
}


For more information please refer to: http://www.dotnet-tricks.com/Tutorial/mvc/D8I4270712-MVC-Data-Annotations-for-Model-Validation.html


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.