0
我使用ValidationContext將驗證代碼添加到我的對象類中。我添加了以下內容,但沒有一個驗證屬性會觸發。到目前爲止,我 不知道爲什麼。任何幫助表示讚賞。asp.net mvc無法使用ValidationContext驗證輸入DTO對象
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>();
}
}
您已經應用到您的模特屬性'([必需]'驗證屬性,'[StringLength]'等)。你想通過實現'IValidatableObject'來做什麼? –