DataType vs UiHint

后端 未结 2 1389
一向
一向 2021-02-13 01:12

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         


        
2条回答
  •  一整个雨季
    2021-02-13 02:12

    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?

提交回复
热议问题