【发布时间】:2012-08-17 11:44:49
【问题描述】:
我有一个奇怪的问题。 ModelState 有错误。但我对此没有规定。没有过滤器,验证器文件中没有规则。
我的代码。视图模型:
[Validator(typeof(TestValidation))]
public class PayerPayRateViewModel
{
public int TestId { get; set; }
public bool AllServices { get; set; }
public int ParentEntityId { get; set; }
}
验证器
public class TestValidation : BaseEntityRepositoryValidator<Core.Domain.Model.Entities.Payer, PayerPayRateViewModel>
{
public TestValidation()
{
RuleFor(x => x.ParentEntityId).Must(CheckUniqueService);
}
protected bool CheckUniqueService(PayerPayRateViewModel model, int value)
{
if (model.AllServices)
{
return true;
}
return false;
}
}
如果我有值为 0 的 TestId,我会得到“TestId: Field is required”。
当我从 Viewmodel 类中删除验证属性时,我收到“A value is required.”错误。
为什么会这样?
【问题讨论】:
标签: asp.net-mvc-3 fluentvalidation