gpt4 book ai didi

c# - DataType(DataType.*) 的用途是什么?

转载 作者:行者123 更新时间:2023-12-03 08:36:15 29 4
gpt4 key购买 nike

我使用了像[EmailAddress]这样的数据属性和[DatabaseGenerated(DatabaseGeneratedOption.None)]用于 asp.net core mvc 中的服务器端验证和 Entity Framework Core 中的映射,但从未 DataType(DataType.Example) ,我尝试搜索它,但我仍然不明白它的概念或目的。
所以我正在寻找的是关于它的实际使用示例的解释。您可以使用以下示例作为引用。

    public class ProvidingContext
{
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }

[DataType(DataType.Url)]
public string Url { get; set; }

[DataType(DataType.Currency)]
public double Price { get; set; }
}

最佳答案

该属性仅仅是一个注释(它讲述有关属性的一些信息,但本身不执行任何操作),并且 documented as such :

Represents an enumeration of the data types associated with data fields and parameters.

MVC 用它做什么,并没有真正记录下来。有is an official MVC tutorial that explains some of its usage though :

The DataType attributes only provide hints for the view engine to format the data (and supplies elements/attributes such as for URL's and for email. You can use the RegularExpression attribute to validate the format of the data. The DataType attribute is used to specify a data type that's more specific than the database intrinsic type, they're not validation attributes. In this case we only want to keep track of the date, not the time.

The DataType Enumeration provides for many data types, such as Date, Time, PhoneNumber, Currency, EmailAddress and more. The DataType attribute can also enable the application to automatically provide type-specific features. For example, a mailto: link can be created for DataType.EmailAddress, and a date selector can be provided for DataType.Date in browsers that support HTML5.

The DataType attributes emit HTML 5 data- (pronounced data dash) attributes that HTML 5 browsers can understand. The DataType attributes do not provide any validation.

另请参阅:

所以:Razor 使用它来呈现属性,以便浏览器可以显示 native 验证和选择控件(例如日期选择器、数字输入等)。

关于c# - DataType(DataType.*) 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63802245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com