DataType vs UiHint

混江龙づ霸主 提交于 2019-12-09 08:07:18

问题


I have been using mvc2 for a while now, and when i need to set the template i use the DataType Attribute

    [DataType("DropDown")]
    public int Field { get; set; }

I see others using UiHint to achieve the same results

    [UiHint("DropDown")]
    public int Field { get; set; }

What is the difference between using these two attributes? Which attribute should I be normally using, or are they for different tasks?


回答1:


DataType is generally used to make it known that this is a very specific version of a property, such as price.

The most common example of DataType is the [DataType(DataTypes.EmailAddress)] which usually is a string but we're saying that this is a very specific type of string.

They're both helpful and the UIHint overrides the DataType. So if you have a certain DataType but you want to override the editor for that specific property you can use a UIHint.




回答2:


DataType attribute has two purposes

  • Provide additional type information for a data field. You do this by applying the DataTypeAttribute attribute to a data field in the data model and by specifying the additional type name from the DataType enumeration. Then the view engine uses the default template for displaying the property, like, a checkbox for a boolean.
  • If you want to override the default template, and wish to use a custom template, then it can be used to associate a custom field template with that data field. In this case you must provide a partial page[.cshtml, MVC 4] to describe the display.
  • The purpose of UIHint is exactly same as the second point above. Where to use what? The answer is: context, ie., what will make more sense, what is closer to the physical problem your code is trying to solve. What if both are applied to the same property? The answer is: UIHint has precedence, obviously. But why would you apply both?



    来源:https://stackoverflow.com/questions/3770721/datatype-vs-uihint

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!