Data Annotation Examples
A few Data Annotations that come in handy when decorating models
- [Key] Useful if the key on the table is not explicitly obvious or declared
- [Required] Forces a value - uses a default error message of "Field Name is Required"
- [Display(Name = "Phone")] This is what shows on a LabelFor
- [RegularExpression(@"^[^<>*]+$", ErrorMessage = "No Special characters allowed")] Regex expressions can provide lots of flexibility
- [DataType(DataType.EmailAddress)] Various DataTypes are available[DataType(DataType.PostalCode)]
- [MaxLength(30, ErrorMessage = "City cannot exceed 30 characters")]
Maxlength annoations do not stop entry at 30 characters like the client side version but does throw an error if field exceeds # of charaters
Comments