什么是[数据类型(DataType.EmailAddress)和放大器之间的差异; [电子邮件地址] [英] what are the differences between [DataType(DataType.EmailAddress)] & [EmailAddress]
问题描述
我想了解什么是使用之间的主要differecnes [数据类型(DataType.EmailAddress)]
&安培; [EmailAddress的]
。
I am trying to understand what are the main differecnes between using
[DataType(DataType.EmailAddress)]
& [EmailAddress]
.
模型类中: -
public class MYViewModel {
[DataType(DataType.EmailAddress)] OR [EmailAddress]
public string Email { get; set; }
我做了一个测试,两个属性将做到以下几点: -
i did a test and the two attributes will do the following:-
-
将prevent用户添加invalud电子邮件地址
will prevent users from adding invalud email address
将显示值EmailTo:......
will display the value as "EmailTo:..."
但如果我用我找不到关于功能,当然任何差异, html.TextboxFor
那么数据类型
将不会有任何效果,而如果我使用 html.EditorFor
然后将数据类型的数据注解将工作,,但我就讲的差异的技术执行?
but i can not find any differences in respect to the functionality , of course if i use html.TextboxFor
then the Datatype
will not have any effect, while if i use html.EditorFor
then the Datatype data annotation will work,, but i am talking about the differences in respect to the technical implementation ?
推荐答案
希望这澄清了...
正如你提到的,数据类型
属性主要用于格式化,无法验证。它之所以似乎工作是:
As you noted, DataType
attributes are primarily used for formatting, not validation. The reason it seems to work is:
-
@ Html.EditorFor
渲染HTML5<输入类型=电子邮件......
会推迟到客户端/浏览器做验证。如果浏览器符合,然后出现客户端验证。它将工作,因为客户端验证为你(这是不会服务器端验证不过)
@Html.EditorFor
renders the HTML5<input type="email" ....
which defers to the client/browser to do validation. If the browser complies, then client side validation occurs. It will "work" because the client validated it for you (this is not server side validation however)
您可以通过在图中修改 @ Html.EditorFor
到 @ Html.TextBoxFor
测试,这将呈现输入字段为&LT;输入类型=文本...&GT;
(一个标准的文本输入框,而不是HTML5 电子邮件
)。
You can test it by changing @Html.EditorFor
to @Html.TextBoxFor
in your view, which will render the input field as <input type="text" ...>
(a standard text input field, not HTML5 email
).
由于像这样一个模型:
public class User
{
[Required(ErrorMessage = "Email must be provided")]
[DataType(DataType.EmailAddress, ErrorMessage = "this doesn't do email format validation")]
[EmailAddress(ErrorMessage = "Not a valid Email")] //Comment un-comment to see effect
public string EmailAddress { get; set; }
[Required(ErrorMessage = "Name must be provided")]
public string Name { get; set; }
}
使用视图 @ Html.TextBoxFor
而不是 @ Html.EditorFor
拿出HTML5客户端验证中测试:
A view using @Html.TextBoxFor
instead of @Html.EditorFor
to take out HTML5 client side validation in your test:
@Html.TextBoxFor(model => model.EmailAddress,....
和像这样的控制器:
public ActionResult CheckUser(User user)
{
ViewBag.Foo = string.Empty;
if(Request.HttpMethod == HttpMethod.Post.ToString())
{
ViewBag.Foo = ModelState.IsValid ? "User Model validated" : "Failed Model Validation";
}
return View();
}
如果您:
- 的注释的
[EmailAddress的]
属性,只留下[数据类型(DataType.EmailAddress)]
您的模型的有效用任何文本(没有电子邮件格式验证)- 如果你把富的模式是有效的,没有错误消息。
- comment out
[EmailAddress]
attribute, leaving only[DataType(DataType.EmailAddress)]
your model is valid with any text (no email format validation)- if you put "foo" your model is "valid", no error message.
- 如果你把富,它会失败,并显示
无效的电子邮件
错误信息
- if you put "foo", it will fail and the
"Not a valid Email"
error message is displayed
Hth以上...
这篇关于什么是[数据类型(DataType.EmailAddress)和放大器之间的差异; [电子邮件地址]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!