0
是否可以在同一個ViewModel中運行FluentValidation 和 IValidatableObject?例如,如果我有以下視圖模型:結合FluentValidation和IValidatableObject錯誤
public class TestViewModel
{
public Foo Foo { get; set; }
public Bar Bar { get; set; }
}
public class Foo : IValidatableObject
{
public string FooName { get; set; }
public IEnumerable<ValidationResult> Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext)
{
if (string.IsNullOrEmpty(FooName))
{
yield return new ValidationResult("Required from IValidatableObject", new[] { "FooName" });
}
}
}
[Validator(typeof(BarValidator))]
public class Bar
{
public string BarName { get; set; }
}
public class BarValidator : AbstractValidator<Bar>
{
public BarValidator() {
RuleFor(x => x.BarName).NotNull().WithMessage("Required from FluentValidation");
}
}
有沒有辦法既Foo
和Bar
驗證可運行並返回結果時,我的控制器調用ModelState.IsValid
?