基于属性的验证和流畅的验证不需要';似乎无法使用asp.net-mvc
我在本教程中遵循了所有这些步骤: 创建了一个验证器类基于属性的验证和流畅的验证不需要';似乎无法使用asp.net-mvc,asp.net-mvc,fluentvalidation,fluentvalidation-2.0,Asp.net Mvc,Fluentvalidation,Fluentvalidation 2.0,我在本教程中遵循了所有这些步骤: 创建了一个验证器类 public class ProjectValidator : AbstractValidator<ProjectViewModel> { public ProjectValidator() { //RuleFor(h => h.Milestone).NotEmpty().WithName("Milestone"); RuleFor(h => h.Applications
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");
}
}
但是,当我查看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)
{
我不确定FundingTypeId是什么类型的HTML元素,但我假设它是一个下拉列表。如果未选择任何内容,则会出现此错误。不幸的是,这是FV与MVC集成的限制之一,这是由MVC的默认模型绑定器的糟糕设计造成的。该消息不是由FV生成的,而是由DefaultModelBinder生成的,在这种情况下,传入值无法转换为属性类型 查看我在Fluent验证论坛上发布的以下两个问题:
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)
{
ProjectValidator validator = new ProjectValidator();
ValidationResult result = validator.Validate(entity);