我使用ValidationContext将验证代码添加到我的对象类中。我添加了以下内容,但没有一个验证属性是触发器。到目前为止,我 无法理解为什么。任何帮助表示赞赏。
public bool Update Book(BookCommand command)
{
var context = new ValidationContext(command, serviceProvider:null, items: null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(command, context, results);
return UpdateBook(BookTitle , BookCode , AuthorFullName);
}
public class BookCommand : Command, IValidatableObject
{
[Required]
public string BookTitle { get; set; }
[Required]
public string BookCode { get; set; }
[Required]
[StringLength(16, MinimumLength = 8, ErrorMessage = "Length must be 8 to 16 characters long")]
public string AuthorFullName{ get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
return new List<ValidationResult>();
}
}
答案 0 :(得分:0)
如果我记得很清楚你必须选择属性或IValidatableObject
删除IValidatableObject接口,它应该完成工作