Types of Data Annotation in MVC


Annotations are spread across a variety of namespaces, including [html]System.ComponentModel[/html] and [html]System.ComponentModel.DataAnnotations[/html]. If you explore these namespaces, you can find even more attributes, but some of them don’t seem to work with ASP.NET MVC, at least not in the way that you would expect.

Data Annotation:

Data annotations are attributes, and attributes don’t typically contain code. They just represent meta information for other modules to consume. By using data annotations, you decorate your model objects with metadata. This isn’t expected to produce any visible effect: It all depends on how other components consume metadata.

Types of Data Annotation:

DataType: It Indicates the presumed data type you’ll be editing through the member. It accepts values from the DataType enumeration. Supported data types include Decimal, Date, DateTime, EmailAddress, Password, Url, PhoneNumber, and MultilineText.

DisplayFormat: It uses to indicate a format through which to display (and/or edit) the value.

DisplayName: It Indicates the text to use for the label that presents the value.

HiddenInput: It Indicates whether a hidden input field should be displayed instead of a visible one.

UIHint: It Indicates the name of the custom HTML template to use when displaying or editing the value.


public class CustomerViewModel : ViewModelBase
[DisplayName("Company ID")]
[ReadOnly(true)] // This will be blissfully ignored by default templates!
public Int32 Id { get; set; }
[DisplayName("Is a Company (or individual)?")]
public Boolean IsCompany { get; set; }
[DisplayFormat(NullDisplayText = "(empty)")]
public String Name { get; set; }
public String Notes { get; set; }
public String Website { get; set; }
[DisplayName("Does this customer pay regularly?")]
public Boolean? reliable { get; set; }