开发者

ASP.NET MVC must match validation attribute

I cant seem to find the annotation to use that makes sure 2 or more textboxes are the same.

For ex:

public class NewPasswordModel
开发者_开发知识库{
    public string NewPassword { get; set; }

    [MustMatch(Name="NewPassword")] // What is the correct thing to come here.
    public string NewPasswordRep { get; set; }
}


You can use the native CompareAttribute

public class NewPasswordModel
{
    public string NewPassword { get; set; }

    [Compare("NewPassword")]
    public string NewPasswordRep { get; set; }
}


You can install the DataAnnotationsExtensions.MVC3 nuget package, and use the EqualToAttribute.

public class NewPasswordModel
{
    public string NewPassword { get; set; }

    [EqualTo("NewPassword")]
    public string NewPasswordRep { get; set; }
}

It provides scripts for unobtrusive jQuery validation, so client-side validation will work as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜