MVC数据注释将一个属性与另一个属性进行比较?

dev*_*ife 17 asp.net-mvc data-annotations

我一直在玩MVC2中的数据注释,如果有一个注释来比较2个属性(即密码,确认密码),我很好奇吗?

Cil*_*lan 42

如果您使用的是ASP.Net MVC 3,则可以使用 System.Web.Mvc.CompareAttribute

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirm { get; set; }
Run Code Online (Sandbox Code Playgroud)

  • 在.Net 4.5中,它也在System.Component.DataAnnotations中. (11认同)
  • 为什么这是在system.web.mvc而不是dataAnnotations?不应该在我的模型项目中引用system.web.mvc.多烦啊 (5认同)