比较属性DataAnnotations无效

时间:2014-01-13 09:42:48

标签: c# asp.net-mvc data-annotations

当我手动验证对象时,我无法使compare属性工作。我做了一个简单的测试控制台应用程序也无法正常工作。我有些不对劲吗?

我使用最新版本的.Net Framework 4.5.1。 我制作了这个控制台测试应用程序,因为它也不能在我的MVC应用程序中工作,该应用程序在业务层(单独的类库)中执行数据注释。

感谢。

要测试的课程:

public class Change // : IValidatableObject
{
    /// <summary>
    /// The current password of this account.
    /// </summary>
    [Required(ErrorMessage = "Huidig wachtwoord is verplicht")]
    [DataType(DataType.Password)]
    public string CurrentPassword { get; set; }

    /// <summary>
    /// The new password for the logged in user account.
    /// </summary>
    [Required(ErrorMessage = "Wachtwoord is verplicht")]
    [DataType(DataType.Password)]
    public string NewPassword { get; set; }

    /// <summary>
    /// This must be the same as <see cref="NewPassword"/>.
    /// </summary>
    [Required(ErrorMessage = "Bevestig wachtwoord is verplicht")]
    [Compare("NewPassword")]
    [DataType(DataType.Password)]
    public string NewPassword2 { get; set; }

    //public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    //{
    //    return new List<ValidationResult>();
    //}
}

控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        var change = new Change()
        {
            CurrentPassword = "ABC",
            NewPassword = "123",
            NewPassword2 = "12345678"
        };

        Console.WriteLine("Initial values:");
        Console.WriteLine("NewPassword: " + change.NewPassword);
        Console.WriteLine("NewPassword Confirm: " + change.NewPassword2);
        Console.WriteLine();

        Console.WriteLine("Let's see if the compare attribute works...");
        Console.WriteLine("----------------------------------------------");
        Console.WriteLine();

        try
        {
            Validator.ValidateObject(change, new ValidationContext(change, null, null));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();

            throw ex;
        }

        Console.WriteLine("Validation doesn't work because you see this line.");
        Console.ReadLine();
    }
}

添加IValidatableObject也不起作用。

3 个答案:

答案 0 :(得分:1)

试试这个

 private List<ValidationResult> ValidateModel(object model)
        {
            var validationResults = new List<ValidationResult>();
            var ctx = new ValidationContext(model, null, null);
            Validator.TryValidateObject(model, ctx, validationResults, true);
            return validationResults;
        }

答案 1 :(得分:1)

尝试使用带有Validator.ValidateObject参数的Boolean的其他重载来验证所有属性:

public static void ValidateObject(
  Object instance,
  ValidationContext validationContext,
  bool validateAllProperties
)

参考:Validator.ValidateObject

示例:

try
{
  Validator.ValidateObject(change, new ValidationContext(change, null, null), true);
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
  Console.ReadLine();

  throw ex;
}

答案 2 :(得分:1)

调用Validator.ValidateObject(更改,新的ValidationContext(更改),true)对我有用,boolean告诉Validator验证所有属性。