我使用简单的流畅验证。如果仅当Notes Textbox为空时如何进入此规则,并且如果该linq查询返回记录则应返回验证。
如果文本框不为空,则不应进入此规则。
这是我尝试的内容
RuleFor(i => i.Notes)
.NotEmpty()
.When((i) =>
{
bool result = false;
result = _DAL.GetExists<EmployeeScheduleTypes>
(q => q.Type == i.Type);
return result;
})
.WithMessage("Notes is required");
答案 0 :(得分:1)
应该使用扩展方法When
来指定运行属性验证程序时的条件。在您的情况下,条件不是空Notes
属性。验证谓词应转到Must
扩展名:
RuleFor(i => i.Notes)
.Must(i => _DAL.GetExists<EmployeeScheduleTypes>(q => q.Type == i.Type))
.When(i => String.IsNullOrEmpty(i.Notes))
.WithMessage("Notes is required");
此规则表示:当Notes为空时,对象应具有现有类型,否则为Notes属性设置错误。