Friday, 27 September 2013

Data Annotations Attributes for Formatting Purpose

There are many attributes that we can use from System.ComponentModel.DataAnnotations. A few of them have just been added in the latest framework. Some of those are useful for validation and others for formatting purpose. On this post, I'd like to particularly focus on the attributes that can be used for formatting/displaying data.

However, first I will list down the ones that are useful for validation:
- Compare
- CustomValidation (this is different from creating custom validation class that derives from ValidationAttribute class)
- EmailAddress
- FileExtensions
- MaxLength
- MinLength
- Phone
- Range
- RegularExpression
- Required
- StringLength
- Url

Then the ones that are useful for formatting are below:
- Display
Specify the property name to be displayed. Also can be used to set the display order of the property. The default value is 10,000.
e.g.;
[Display(Name=”First Name”, Order=11000)]

- ScaffoldColumn
Hide the property to be displayed by EditorForModel and DisplayForModel.
e.g.;
[ScaffoldColumn(false)]

- DisplayFormat
Display the property in specific format in EditorFor or DisplayFor helper. Some options are:
ConvertEmptyStringToNull, NullDisplayText, DataFormatString, ApplyFormatInEditMode and HtmlEncode.
e.g.;
[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "[Null]")]
ConvertEmptyStringToNull will automatically convert passed empty string into null when binding model.
NullDisplayText is used to specify text to be displayed if the property value is null. By default an empty string will be displayed.
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString=”{0:c}”)]
ApplyFormatInEditMode by default is false because most of the time the input passed from edit form would not be able to be parsed.

- ReadOnly
Tell model binder to not set the property with a new value. EditorForModel will still enable the input to be edited however the model binder will ignore the value passed.

- DataType
Set the property to be displayed in a more specific data type. Some of the data types are; MultilineText, Password and Url. The complete list of data types and their description can be found here.
e.g.;
[DataType(DataType.Password)]

- UIHint
Used to specify custom template name to be used by EditorFor or DisplayFor helper when rendering the property. This attribute is useful when we want to use a custom template for displaying a property. The template is put under DisplayTemplates or EditorTemplates folder under Shared or a specific controller folder.

For example; if we create a new template for displaying hyperlink in a file called Hyperlink.cshtml:
<a href="@Model" target="_blank">@Model</a>
Then we can add this attribute on property that we want to be rendered using the new template. If we have put the template under Shared folder then this can be applied to any property in the project.
[UIHint("Hyperlink")]

- HiddenInput
Set the property to be rendered as an html hidden element.

No comments: