数据类型与 UiHint

发布于 2024-09-24 23:01:08 字数 319 浏览 0 评论 0原文

我已经使用 mvc2 一段时间了,当我需要设置模板时,我使用 DataType 属性

<前><代码> [数据类型(“DropDown”)] 公共 int 字段 { 获取;放; }

我看到其他人使用 UiHint 来达到相同的结果

 [UiHint("DropDown")]
    公共 int 字段 { 获取;放; }

使用这两个属性有什么区别?我通常应该使用哪个属性,或者它们用于不同的任务?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-10-01 23:01:08

DataType 通常用于表明这是属性的非常具体的版本,例如价格。

DataType 最常见的示例是 [DataType(DataTypes.EmailAddress)],它通常是一个字符串,但我们说这是一种非常特定的字符串类型。

它们都很有帮助,并且 UIHint 覆盖 DataType。因此,如果您有特定的数据类型,但您想覆盖该特定属性的编辑器,则可以使用 UIHint。

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.

海夕 2024-10-01 23:01:08

DataType 属性有两个用途

  • 为数据字段提供附加类型信息。您可以通过将 DataTypeAttribute 属性应用到数据模型中的数据字段并指定 DataType 枚举中的附加类型名称来完成此操作。然后视图引擎使用默认模板来显示属性,例如布尔值的复选框。
  • 如果您想要覆盖默认模板,并希望使用自定义模板,则可以使用它来将自定义字段模板与该数据字段相关联。在这种情况下,您必须提供部分页面[.cshtml,MVC 4]来描述显示。
  • UIHint的目的与上面第二点完全相同。
    哪里用什么?答案是:上下文,即什么更有意义,什么更接近您的代码试图解决的物理问题。
    如果两者都应用于同一个属性怎么办?答案是:显然 UIHint 具有优先权。但为什么要同时应用两者呢?

    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?

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文