0

I have two models :

public class CardPayment
{
   [Required]
    public Card CardDetail { get; set; }

    [Required]
    [MaxLength(100)]
    public string Description { get; set; }
}
public class Card
{
    [Required]
    [MaxLength(50)]
    public string CardHolder { get; set; }

    [Required, MaxLength(3)]
    public string Cv2 { get; set; }
}

I am validating the models with the following bit of code

var context = new ValidationContext(cardPayment);
var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();

if (Validator.TryValidateObject(cardPayment, context, results, true))
{
}

If i pass in a model with a description that has 101+ characters the validation works and the results collection has the validation error.

If i pass in the same model but set the Cv2 field to be 4+ characters it does not get picked up.

Is it possible for the TryValidateObject to validate the inner model?

Neil Gilbert
  • 125
  • 1
  • 9
  • https://stackoverflow.com/questions/7663501/dataannotations-recursively-validating-an-entire-object-graph – vasily.sib May 31 '18 at 09:49
  • 1
    Possible duplicate of [DataAnnotations: Recursively validating an entire object graph](https://stackoverflow.com/questions/7663501/dataannotations-recursively-validating-an-entire-object-graph) – vasily.sib May 31 '18 at 09:50

0 Answers0