1
我遵循了所有在本教程下列步骤来:基于属性的验证似乎不使用asp.net-MVC
创建一个验证器类
public class ProjectValidator : AbstractValidator<ProjectViewModel>
{
public ProjectValidator()
{
//RuleFor(h => h.Milestone).NotEmpty().WithName("Milestone");
RuleFor(h => h.Applications).NotNull().WithName("Application");
RuleFor(h => h.InitiativeName).NotNull().WithName("Business Aligned Priority");
RuleFor(h => h.BusinessDriverId).NotNull().WithName("Business Driver");
RuleFor(h => h.FundingTypeId).NotNull().WithName("Funding Type");
RuleFor(h => h.Description).NotEmpty().WithName("Description");
RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
RuleFor(h => h.Sponsors).NotNull().WithName("Sponsors");
}
}
把一个属性上我的DTO具体这个validtor
[Validator(typeof(ProjectValidator))]
public class ProjectViewModel
{
}
但后一种形式后,当我去检查的ModelState错误列表中,我看到的错误是从asp.net MVC的默认验证到来。
public ActionResult UpdateMe(ProjectViewModel entity)
{
Project existingProject = this.Repository.Fetch<Project>(entity.Id);
UpdateProperties(entity, existingProject);
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
if (allErrors.Count() > 0)
{
对于为什么它没有采取流利的任何建议。验证器 ??我已经添加下面的图片是我在GUI
看看我直接在代码中调用验证它工作得很好:
ProjectValidator validator = new ProjectValidator();
ValidationResult result = validator.Validate(entity);