DataType.EmailAddress无法在asp.net mvc 3中运行 [英] DataType.EmailAddress not working in asp.net mvc 3

查看:44
本文介绍了DataType.EmailAddress无法在asp.net mvc 3中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的表单上使用MVC,我的模型如下



Hi, I am using MVC on my form and my Model is as follows

 [Required(ErrorMessage = "E-mail must be entered")]
[DataType(DataType.EmailAddress,ErrorMessage="invalid")]
public string UserName { get; set; }





它正在检查针对Required属性的验证,但不针对数据类型。谁有人解释为什么?添加了对DataAnnotation.dll的引用,覆盖了RegularExpressionAttribute类的IsValid方法......没有结果..呵呵

谢谢。



Its is checking validation against Required attribute but not against datatype. Could anyone explain why? Added reference to DataAnnotation.dll, overrided IsValid method of RegularExpressionAttribute class... No results.. huh
Thank you.

推荐答案

[DataType(DataType.EmailAddress,ErrorMessage="invalid")]



这不足以在字段上设置验证。

对于电子邮件验证,请使用正则表达式作为验证。



http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model- validation.aspx [ ^ ]是电子邮件验证的一个很好的例子。


This is not sufficient to set a validation on a field.
For email validation, use regular expressions as validation.

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx[^] is a good example on email validation.


显然这有效:



Apparently this works:

using System.ComponentModel.DataAnnotations;
    [Required]
    [EmailAddress]
    public String Email { get; set; }


你可以尝试



you can try

[EmailAddress]










[DataType(DataType.EmailAddress,ErrorMessage="invalid")] this way not work


这篇关于DataType.EmailAddress无法在asp.net mvc 3中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆