[DataType(DataType.EmailAddress)]和& amp;之间有什么区别?[电子邮件地址] [英] what are the differences between [DataType(DataType.EmailAddress)] & [EmailAddress]

查看:65
本文介绍了[DataType(DataType.EmailAddress)]和& amp;之间有什么区别?[电子邮件地址]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解使用之间的主要区别是什么 [DataType(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:-

  1. 将阻止用户添加宝贵的电子邮件地址

  1. will prevent users from adding invalud email address

将值显示为"EmailTo:..."

will display the value as "EmailTo:..."

但是我找不到关于功能的任何差异,当然,如果我使用 html.TextboxFor ,那么 Datatype 不会有任何影响,而如果我使用使用 html.EditorFor 然后可以使用Datatype数据注释,但是我在谈论技术实现方面的差异?

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 ?

推荐答案

希望这可以澄清...

Hope this clarifies...

如您所述, DataType 属性主要用于格式化,而非验证.它似乎正常工作的原因是:

As you noted, DataType attributes are primarily used for formatting, not validation. The reason it seems to work is:

  • @ Html.EditorFor 呈现HTML5 < input type ="email" .... ,该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 进行测试,这会将输入字段呈现为< input type="text" ...> (标准文本输入字段,而不是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).

给出具有以下内容的模型:

Given a model with something like this:

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,....

还有一个像这样的控制器:

And a controller like so:

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();
}

如果您:

  1. 注释掉 [EmailAddress] 属性,仅保留 [DataType(DataType.EmailAddress)] 您的模型有效 "(不验证电子邮件格式)
    • 如果您输入"foo",则模型为有效",没有错误消息.
  1. 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.
  • 如果您输入"foo",它将失败并显示无效的电子邮件" 错误消息

Hth ...

这篇关于[DataType(DataType.EmailAddress)]和&amp; amp;之间有什么区别?[电子邮件地址]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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