与DataAnnotations和数据类型的电子邮件模型验证 [英] Email model validation with DataAnnotations and DataType
问题描述
我有以下型号:
public class FormularModel
{
[Required]
public string Position { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
public string Webcode { get; set; }
}
需要确认工作正常。但是当我尝试数据类型不发生反应。
Required validation works fine. But when i try with DataType it doesn't react.
下面是我的剃须刀code的电子邮件控件:
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
attribute is used for formatting purposes, not for validation.
我建议你使用 ASP.NET MVC 3期货交易电子邮件验证。
I suggest you use ASP.NET MVC 3 Futures for email validation.
样code:
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
如果你碰巧使用 .NET框架4.5
,现在有一个在 EmailAddressAttribute
建,生活在<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.emailaddressattribute.aspx\"><$c$c>System.ComponentModel.DataAnnotations.EmailAddressAttribute$c$c>.
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和数据类型的电子邮件模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!