我有两种模式:
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; }
}
我正在使用以下代码
验证模型var context = new ValidationContext(cardPayment);
var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
if (Validator.TryValidateObject(cardPayment, context, results, true))
{
}
如果我传入一个包含101个以上字符的描述的模型,则验证有效,结果集合会出现验证错误。
如果我传入相同的模型,但将Cv2字段设置为4+个字符则不会被拾取。
TryValidateObject是否可以验证内部模型?