问题描述
我有以下型号:
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 时它没有反应.
Required validation works fine. But when i try with DataType it doesn't react.
这是我用于电子邮件控件的剃刀代码:
Here is my razor code for the email control:
@Html.TextBoxFor
(model => model.Email,
new { @style = "width: 175px;", @class = "txtField" }
) *
那么,有人知道答案吗?
So, anyone know an answer?
TIA
推荐答案
DataType
属性用于格式化目的,而不是用于验证.
DataType
attribute is used for formatting purposes, not for validation.
我建议您使用 ASP.NET MVC 3 Futures 进行电子邮件验证.
I suggest you use ASP.NET MVC 3 Futures for email validation.
示例代码:
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
如果您碰巧在使用 .NET Framework 4.5
,现在有一个内置的 EmailAddressAttribute
位于 System.ComponentModel.DataAnnotations.EmailAddressAttribute
.
If you happen to be using .NET Framework 4.5
, there's now a built in EmailAddressAttribute
that lives in System.ComponentModel.DataAnnotations.EmailAddressAttribute
.
这篇关于使用 DataAnnotations 和 DataType 验证电子邮件模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!