简体   繁体   中英

TryValidateProperty fails with error "'The type 'Customer' does not contain a public property named 'Address.City'. (Parameter 'propertyName')'"

Trying to use TryValidateProperty to validate Customer class, it fails validate on Address property which is another class. I'm getting this error

System.ArgumentException: 'The type 'Customer' does not contain a public property named 'Address.City'. (Parameter 'propertyName')'

    public class Customer
    {
      [Required(ErrorMessage = "{0} is mandatory")]
      [MaxLength(50, ErrorMessage = "The {0} can not have more than {1} characters")]
      public string Name { get; set; }

      [Required(ErrorMessage = "{0} is mandatory")]
      [Range(0, 150, ErrorMessage = "The Age should be between 0 and 150 years")]
      public int Age { get; set; }
    
      [Required(ErrorMessage = "{0} is mandatory")]
      public Address? Addresse { get; set; }

     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
     {
        var results = new List<ValidationResult>();

        Validator.TryValidateProperty(this.Name,
            new ValidationContext(this, null, null) { MemberName = "Name" },
            results);
        Validator.TryValidateProperty(this.Age,
            new ValidationContext(this, null, null) { MemberName = "Age" },
            results);
        Validator.TryValidateProperty(this.Addresse.City, new ValidationContext(this, null, null) { MemberName = "Address.City" }, results);

        return results;
      }
    }
    
   var customer = new Customer()
   {
      //Name = "hello",
      EntryDate = DateTime.Today,
      Password = "AAAA",
      PasswordConfirmation = "BBBB",
      Age = -1,
      Addresse = new Address()
      {
          //City = "CTG",
          Street = "Goli"
      }
  };

  var validationContext = new ValidationContext(customer);
  var resultAdd = customer.Validate(validationContext);

How to validate the Address properties? TIA.

I got it working making the following changes in Validate method. Need to pass the context correctly to the Validator.

public class Customer
{
  [Required(ErrorMessage = "{0} is mandatory")]
  [MaxLength(50, ErrorMessage = "The {0} can not have more than {1} characters")]
  public string Name { get; set; }

  [Required(ErrorMessage = "{0} is mandatory")]
  [Range(0, 150, ErrorMessage = "The Age should be between 0 and 150 years")]
  public int Age { get; set; }

  [Required(ErrorMessage = "{0} is mandatory")]
  public Address? Addresse { get; set; }

  public IEnumerable<ValidationResult> Validate()
  {
    var results = new List<ValidationResult>();

    Validator.TryValidateProperty(this.Name,
        new ValidationContext(this, null, null) { MemberName = "Name" },
        results);
    Validator.TryValidateProperty(this.Age,
        new ValidationContext(this, null, null) { MemberName = "Age" },
        results);
    Validator.TryValidateProperty(this.Addresse.City, new ValidationContext(this.Address, null, null) { MemberName = "City" }, results);

    return results;
  }
}

var customer = new Customer()
{
  //Name = "hello",
  EntryDate = DateTime.Today,
  Password = "AAAA",
  PasswordConfirmation = "BBBB",
  Age = -1,
  Addresse = new Address()
  {
      //City = "CTG",
      Street = "Goli"
  }
};
var result = customer.Validate();

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM