c# - 使用 DataAnnotations 和 DataType 进行电子邮件模型验证

标签 c# asp.net-mvc validation email model

我有以下模型:

public class FormularModel
{
    [Required]
    public string Position { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [Required]
    public string Webcode { get; set; }
}

必需的验证工作正常。但是当我尝试使用 DataType 时,它​​没有反应。

这是我的电子邮件控制代码:

   @Html.TextBoxFor
          (model => model.Email, 
           new { @style = "width: 175px;", @class = "txtField" }
          ) * 

那么,有人知道答案吗?

TIA

最佳答案

DataType 属性用于格式化,不用于验证。

我建议你使用 ASP.NET MVC 3 Futures用于电子邮件验证。

示例代码:

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

如果您碰巧使用 .NET Framework 4.5,现在有一个内置的 EmailAddressAttribute 位于 System.ComponentModel.DataAnnotations.EmailAddressAttribute 中.

关于c# - 使用 DataAnnotations 和 DataType 进行电子邮件模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989081/

相关文章:

asp.net-mvc - mvc 5 的单声道实现预计何时发布?

php - 为什么我的页面不能在 Internet Explorer 中正确显示?

c# - Interlocked.Exchange 不能与泛型一起使用?

c# - Webbrowser 单击具有 OnClick 属性的链接

c# - 每当我更新 edmx 文件时,它都会更改 model1.context.cs 的类名

c# - 表之间的关系

c# - 自定义数据注释 IsValid 永远不会被调用。 (ASP.NET MVC 2.NET 4)

validation - 我怎样才能输入一个包含@和.com的电子邮件并让我注册

c# - 在 C# 中使用动态访问匿名类型的字段 - 可能吗?

c# - 使用 C# 的 LPStr 返回值调用 C++ 函数