Datatype.EmailAddress在ASP / .NET / MVC使用EmailAddress、Datatype、ASP、MVC

2023-09-04 00:08:41 作者:青梅骑着竹马

我在我使用的电子邮件地址作为用户名的账户型号

I have a Account Model in which I am using Email Address as Username

public class RegisterModel
    {
        [Required]
        [Display(Name = "Email Address")]
        [DataType(DataType.EmailAddress)]
        public string UserName { get; set; }

我设计了一个自定义类来验证电子邮件。但最近,我注意到 DataType.EmailAddress 。我试图用这个数据类型,如图code以上检查,如果我可以验证用户名没有我的自定义类,但它失败。所以我的问题是如何是此数据类型有用的 .NET 。这似乎是无所事事在我的登记表。

I have designed a custom class to verify email. But I recently noticed that the DataType.EmailAddress. I tried to use this Datatype as shown in code above to check if I can validate the Username without my Custom Class but it fails. So my question is how is this DataType useful in .NET. It seems to be doing nothing on my Registration Form.

编辑:它dosent甚至验证对一个正则表达式。例如用户名: SS,SSSSSS,TTTT等所有通关的有效电子邮件

It dosent even validate against a regex. For example Username: SS, ssssss, tttt, etc all pass off as valid emails.

编辑:人无我有一类身后,以证明code电子邮件。我知道帽子是验证电子邮件的复杂性。我不问如何验证邮件。我只是询问此数据类型的应用。

推荐答案

所以,你问这是什么数据类型不为什么不验证,正确吗?每MSDN,数据类型属性主要用于格式化和未验证(你已经学会)。这是什么属性应该做的,就是用 Html.DisplayFor()帮手的时候,渲染领域作为一个可点击的超链接。

So, you are asking what this data type does not why isn't it validating, correct? Per the MSDN, DataType attributes are used primarily for formatting and not validation (which you have learned). What this attribute should do, is when using the Html.DisplayFor() helper, render the field as a clickable hyperlink.

@Html.DisplayFor(x=>x.UserName)

渲染

<a href="mailto:{0}">{0}</a>

此外,如在下面的评论指出Zhaph,在使用它的 Html.EditorFor()将生成一个HTML 5的电子邮件投入,这看起来是这样的

Additionally, as pointed out by Zhaph in the comments below, using it in the Html.EditorFor() will generate an HTML 5 email input, which looks something like this:

<input type="email".../>

从 MSDN

下面的示例使用DataTypeAttribute定制   客户表中的EmailAddress的数据字段的显示   AdventureWorksLT数据库。电子邮件地址显示为   超链接,而不是简单的文本ASP.NET动态数据将   从内部数据类型已推断。

The following example uses the DataTypeAttribute to customize the display of EmailAddress data field of the customer table in the AdventureWorksLT database. The e-mail addresses are shown as hyperlinks instead of the simple text that ASP.NET Dynamic Data would have inferred from the intrinsic data type.

 
精彩推荐
图片推荐