簡體   English   中英

比較屬性DataAnnotations不起作用

[英]Compare attribute DataAnnotations not working

手動驗證對象時,無法使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也不起作用。

嘗試這個

 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;
        }

嘗試使用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;
}

調用Validator.ValidateObject(change,new ValidationContext(change),true)對我有用,布爾值告訴Validator驗證所有屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM