Datatype dataannotation

I read about it on MSDN, and it doesn't describe much. It says that it is used to indicate the type of data to associate with a data column or parameter. Has the meaning. Does this mean that I should use it in all my properties? I guess not. So, why does DataType.Text exist, it seems pretty useless when you have a property as a string.

I found for him the use of multi-line text as a data type to create a text area on the client side, and not just one text input element on one line. But how does this relate to a data column or parameter?

I also suggested that it will also check it, since I used DataType.Url, but it is looking to see if a URL has been entered or not. Same thing with DataType.Email. Is there something I'm doing wrong or do I need to use RegEx instead?

+4
source share
4 answers

See the MSDN article Enumerating a DataType. These are NOT validation attributes. From an MSDN article:

The DataTypeAttribute attribute allows you to mark fields using a type that is more specific than the built-in database types. For example, a string data field containing email addresses of type EmailAddress may be assigned. You can access this information from field templates and change the way the data field is processed. (that is, create a link)

+8
source

The DataType attribute in the context of ASP.NET MVC more or less represents a hint of which view template to use to display the display and editor for this property. As you noticed, it provides an enumeration of the supported data types for which ASP.NET MVC internally provides an editor and display templates (MultilineText โ†’ textarea html input), but can also accept a string parameter with a name of a user type. All he does is search with that name (predefined or missing) in Views/ControllerName/EditorTemplates/{DataTypeName}.cshtml or Views/ControllerName/DisplayTemplates/{DataTypeName}.cshtml when you use the editor / editor for screen or Display / DisplayFor respectively (it will also search in Views / Shared / ...). If it does not fit, it uses the built-in if it is available.

The effect of applying the DataType attribute DataType almost the same as if it were using the UIHint attribute, but it is somewhat more explicit. For example, I would use DataType.Currency for the price and UIHint user interface in the ui user slider.

Regarding verification, the built-in templates provide it, but if you have to use your own data type in your custom display / editor template, you have to provide it yourself.

+5
source

Not quite sure what you are asking for, but DataAnnotations will help you tag properties based on business needs (e.g. email, phone number, etc.)

Refer to DataType Enumeration Members

What is this for?

When you comment on your classes and use them as models in your MVC application. asp.net-mvc will take care of checking (even on the client with javascript, if everything is in place)

See this tutorial for an example: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-6

+1
source

DataTypeAttribute does not check anything, despite inheriting from ValidationAttribute . It overrides the IsValid() method, but simply returns true always.

If you want to check your Email or Url field, use EmailAttribute or UrlAttribute . They inherit from DataTypeAttribute and override the IsValid() method accordingly.

This is the entire list of specific DataType attributes:

  • CreditCardAttribute
  • EmailAddressAttribute
  • EnumDataTypeAttribute
  • FileExtensionsAttribute
  • PhoneAttribute
  • UrlAttribute
+1
source

Source: https://habr.com/ru/post/1398939/


All Articles