开发者

ASP.NET mvc 2 - How to add a "confirm your e-mail address" field?

Let's say I have a model that looks like this:

public class MyModel
{
    [DisplayName("Email:")]
    [Required(ErrorMessage = "Email is required")]
    [Email(ErrorMessage = "Email is invalid")]
    public string Email { get; set; }
}

In ASP.NET MVC 2, I'd render the text box and validation like so:

<%=Html.LabelFor(x => x.Email)%>
<%=Html.TextBoxFor(x => x.Email)%>
<%=Html.Valida开发者_StackOverflow社区tionMessageFor(x => x.Email)%>

How do I add a second field to allow the user to confirm their email address using the display name and validation from the model's property?


Decorate your Class with the following Attribute:

    [PropertiesMustMatch("Email", "ConfirmEmail", ErrorMessage = "The Email Address and confirmation Email Address do not match.")]
    public class MyModel
    {
        [DisplayName("Email:")]
        [Required(ErrorMessage = "Email is required")]
        [Email(ErrorMessage = "Email is invalid")]
        public string Email { get; set; }

        [DisplayName("Confrim Email:")]
        [Required(ErrorMessage = "Email is required")]
        [Email(ErrorMessage = "Email is invalid")]
        public string ConfirmEmail { get; set; }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜