【问题标题】:Does the DataTypeAttribute on a model do validation in MVC 3?模型上的 DataTypeAttribute 是否在 MVC 3 中进行验证?
【发布时间】:2011-07-01 16:13:24
【问题描述】:

默认的 ASP.net MVC 3 Internet Application 模板包括以下模型:

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

在“帐户/注册”操作中,它要求提供电子邮件地址,但您似乎可以在此字段中输入任何内容,它会接受。

DataType(DataType.EmailAddress) 真的会触发验证吗?好像没有。如果它不验证类型,那么它的目的是什么?

【问题讨论】:

  • 我也遇到了同样的问题,你找到解决办法了吗?

标签: c# asp.net-mvc-3 validation


【解决方案1】:

据我所知,DataType 属性用于格式化,但仅在您使用@Html.EditorFor(model => model.Field) 时使用。

来自模型字段、cshtml 和生成的 HTML 的示例:

模型字段:

[Required]
[DataType(DataType.Text)]
[Display(Name = "Name")]
public string Name { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Description")]
public string Desc { get; set; }

cshtml:

<div class="form-section">
    @Html.LabelFor(x => x.Name)
    @Html.EditorFor(x => x.Name)
</div>

<div class="form-section">
    @Html.LabelFor(x => x.Password)
    @Html.EditorFor(x => x.Password)
</div>

<div class="form-section">
    @Html.LabelFor(x => x.Desc)
    @Html.EditorFor(x => x.Desc)
</div>

生成的 HTML:

<div class="form-section">
    <label for="Name">Name</label>
    <input class="text-box single-line" id="Name" name="Name" type="text" value="">
</div>
<div class="form-section">
    <label for="Password">Password</label>
    <input class="text-box single-line password" id="Password" name="Password" type="password" value="">
</div>
<div class="form-section">
    <label for="Desc">Description</label>
    <textarea class="text-box multi-line" id="Desc" name="Desc"></textarea>
</div>

我知道这是一篇旧帖子,但也许我可以为其他人了解情况

编辑:

可能附加了一些验证,但仅限于客户端。不要依赖这些属性进行实际验证,这应该始终在服务器端完成(客户端仅用于用户体验)

【讨论】:

    【解决方案2】:

    当前版本的 MVC3 中没有与 DataType 属性相关的验证。

    从此属性派生的新属性显示在 MVC Futures 中并添加此验证过程。例如,EmailAddressAttribute

    【讨论】:

      【解决方案3】:

      DataType 属性不是验证属性。它们用于模板。前任。您可以为 DataType.Email 使用 html 5 电子邮件标签。 JakeJ 详细说明了。

      【讨论】:

        【解决方案4】:

        默认模板不会将正确的 Validate.js 脚本添加到母版页。数据注释只输出 javascript 读取的 HTML 以进行客户端验证。

        在您的控制器操作中,您需要执行 Model.IsValid 以确保验证服务器端。

        【讨论】:

        • 默认模板添加了所有这些。这就是我想说的。默认的 Internet 应用程序添加客户端脚本...它将 Model.IsValid 添加到控制器操作中,但仍然无法验证。
        【解决方案5】:

        我也无法让该验证器工作,最终编写了我自己的类似于 one found on the ASP.net site 的代码。据说 [EmailAddress] 注释在 MVC3 Future 中可用。

        【讨论】:

          【解决方案6】:

          DataType 属性在使用 editorfor() 时被模板化视图使用

          【讨论】:

            猜你喜欢
            • 2011-01-24
            • 1970-01-01
            • 2011-10-05
            • 2011-12-24
            • 1970-01-01
            • 1970-01-01
            • 2012-06-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多