ASP.NET MVC必须匹配验证属性

时间:2011-03-16 19:27:18

标签: asp.net-mvc asp.net-mvc-3

我似乎无法找到要使用的注释,以确保2个或更多文本框是相同的。

例如:

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

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

2 个答案:

答案 0 :(得分:25)

您可以使用原生CompareAttribute

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

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

答案 1 :(得分:4)

您可以安装DataAnnotationsExtensions.MVC3 nuget包,并使用EqualToAttribute

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

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

它提供了不显眼的jQuery验证脚本,因此客户端验证也可以正常工作。