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?