在 MVC3 中,您可以向模型添加验证以检查属性是否匹配,如下所示:
public string NewPassword { get; set; }
[Compare("NewPassword",
ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
有没有一种方法可以检查两个属性是否不同,就像下面的虚构代码一样?
[CheckPropertiesDiffer("OldPassword",
ErrorMessage = "Old and new passwords cannot be the same")]
public string OldPassword { get; set; }
public string ConfirmPassword { get; set; }
最佳答案
我会检查 Controller 。
在 Controller 中:
if(model.ConfirmPassword == model.OldPassword ){
ModelState.AddModelError("ConfirmPassword", "Old and new passwords cannot be the same");
}
在 View 中:
@Html.ValidationMessage("ConfirmPassword")
希望对你有帮助
关于c# - mvc3 验证检查属性值是否不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8473671/