Data Annotation attributes in MVC3 Razor

1. Data annotations are attributes you can find in the System.ComponentModel.DataAnnotations namespace. 
2. These attributes provide server - side validation and the framework also supports client-side validation when you use one of the attributes on a model property.

Following are the attributes  : 

  • RequiredThis attribute makes the property of model mandatory for user. 
  • StringLengthThe StringLength attribute specifies the maximum length of string that is acceptable.
  • RegularExpressionRegular expressions are an efficient and terse means to enforce the shape and contents of a string value.
  • RangeThe Range attribute specified the minimum and maximum constraints for a numerical value.
  • CompareThe compare attribute ensures two properties on a model object have the same value.
  • DisplayThe Display attribute sets the friendly name for a model property.
  • ScaffoldColumn - The scaffoldColumn attribute hides a property from HTML helper such as EditorForModel and DisplayForModel.
  • MaxLengthMaxLength Attribute ensures the length of the string entered is according to the specified value.
  • DataType - The DataType attribute enables you to provide the run time with information about the specific purpose of a property.
  • UIHint - The UIHint attribute as the name itself suggest gives the MVC run time the name of a template to use when rendering output with the templated helpers like (DisplayFor and EditorFor).
  • HiddenInput - The property that has HiddenInput defined on it will be rendered as input type hidden on form.


Post a Comment