Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatype DataAnnotation

I'm reading on this on MSDN and it doesn't really describe much. It says that it is used to specify the data type to associate with a data column or a parameter. Makes sense. Does this mean I should use it on all of my properties? I assume not. So why is there a DataType.Text, seems pretty useless when you have a property as a string.

I've found a use for it to use multi-line text as a datatype to create a textarea on the client side instead of just a single line text input element. But how does this relate to a data column or paramater?

I also assumed that it would also validate it since I used DataType.Url but it goes through whether or not a Url is entered or not. Same with DataType.Email. Is there something I'm doing wrong or do I need to use a RegEx instead?

like image 369
Shane LeBlanc Avatar asked Feb 29 '12 01:02

Shane LeBlanc


3 Answers

See my MSDN article DataType Enumeration These are NOT validation attributes. From the MSDN article:

The DataTypeAttribute attribute lets you mark fields by using a type that is more specific than the database intrinsic types. For example, a string data field that contains e-mail addresses can be attributed with the EmailAddress type. This information can be accessed by the field templates and modify how the data field is processed. (that is, create a link)

like image 134
RickAndMSFT Avatar answered Oct 31 '22 04:10

RickAndMSFT


The DataType attribute in the context of ASP.NET MVC is more or less a hint of what view template to use for rendering a display and editor for that property. As you have noticed it provides an enumeration of supported data types for which ASP.NET MVC internally provides editor and display templates (MultilineText -> textarea html input), but it can also take a string parameter with a custom type name. All that it does is look for a view with that name (pre-defined or not) in the Views/ControllerName/EditorTemplates/{DataTypeName}.cshtml or Views/ControllerName/DisplayTemplates/{DataTypeName}.cshtml when you use Editor/EditorFor or Display/DisplayFor accordingly (it will also search in Views/Shared/...). If it doesn't fine one it uses a built-in one if available.

The effect of applying a DataType attribute is pretty much the same as if applying the UIHint attribute, but it's a bit more explicit. For example I would use a DataType.Currency on a price and a UIHint on a - say - custom slider ui control model property.

Regarding validation - the build-in templates provide it, but if you were to use a custom datatype in your custom display/editor template you would have to provide that yourself.

like image 25
Ivan Zlatev Avatar answered Oct 31 '22 03:10

Ivan Zlatev


The DataTypeAttribute don't validate anything despite of inheritance from ValidationAttribute. It overrides IsValid() method, but simply returns true always.

If you want to validate your Email or Url field, use EmailAttribute or UrlAttribute instead. These are inherited from DataTypeAttribute and override IsValid() method appropriately.

This is the whole list of specific DataType attributes:

  • CreditCardAttribute
  • EmailAddressAttribute
  • EnumDataTypeAttribute
  • FileExtensionsAttribute
  • PhoneAttribute
  • UrlAttribute
like image 1
Andrey Andreev Avatar answered Oct 31 '22 05:10

Andrey Andreev