mvc [DataType(DataType.EmailAddress) no validation

后端 未结 5 894
情歌与酒
情歌与酒 2020-12-15 16:56

I\'m using this code on an email field:

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = \"Email address\")]
    public string Email          


        
5条回答
  •  不知归路
    2020-12-15 17:35

    It looks like all the answers focus on the Data Model while this issue can be affected by the View itself.

    The following on MVC .NET 4.5 is working alright:

    Data model:

    [Required]
    [DataType(DataType.EmailAddress)]
    [DisplayName("Email")]
    public string Email { get; set; }
    

    Razor View:

    @Html.LabelFor(model => model.Email)
    @Html.EditorFor(model => model.Email)
    

    Note: do not need to add [EmailAddress] attribute. If you use [DataType(DataType.EmailAddress)] along with @Html.EditorFor() in your View, you should be fine.

    As highlighted with rich.okelly, at the end you want your input rendered as .

提交回复
热议问题