c# - mvc3 验证检查属性值是否不同

标签 c# asp.net-mvc-3 validation

在 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/

相关文章:

c# - 不安全代码无法在 Visual Studio 2015 上编译

asp.net-mvc - 过滤器在asp.net mvc中执行的顺序是什么

c# - 属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式

asp.net-mvc - 如何将现有的 asp.net mvc 3 Web 应用程序转换为 azure

validation - 多个字段的 Flask WTform 验证

html - html验证器中的"Stray start tag footer"?

c# - FindObjectOfType 问题

c# - 如何使用 ElementFlow?

javascript - 验证表单中的多个字段

c# - 安全删除目录