0

我想在发布表单时覆盖默认的 asp.net-mvc 验证,所以我尝试使用 fluent.validation

我创建了一个验证器类(ProjectValidator)

public class ProjectValidator : AbstractValidator<Project>
{
    public ProjectValidator()
    {
        RuleFor(h => h.Application).NotNull().WithName("Application");
        RuleFor(h => h.FundingType).NotNull().WithName("Funding Type");
        RuleFor(h => h.Description).NotEmpty().WithName("Description");
        RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
    }
}

我在我的数据传输对象类上放置了一个属性

[Validator(typeof(ProjectValidator))]
  public class ProjectViewModel
  {
      ...
  }

我把它放在 application_start();

 DataAnnotationsModelValidatorProvider
            .AddImplicitRequiredAttributeForValueTypes = false;

 ModelValidatorProviders.Providers.Add(
            new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));

但是当我发布使用此对象的表单时,我收到以下错误:

找不到方法:'System.Collections.Generic.IEnumerable`1 FluentValidation.IValidatorDescriptor.GetValidatorsForMember(System.String)'。

有什么建议么?

4

1 回答 1

2

这可能是与您使用的程序集版本有关的问题。以下是适用于最新版本 FluentValidation.NET 和 ASP.NET MVC 3 的步骤:

  1. 使用默认的 Visual Studio 模板创建一个新的 ASP.NET MVC 3 项目。
  2. 安装FluentValidation.MVC3NuGet 包。
  3. 添加视图模型及其相应的验证器(请注意,在您的情况下,您有Project类型的验证器-AbstractValidator<Project>而您的视图模型被调用ProjectViewModel这是不一致的。验证器必须与视图模型相关联):

    public class ProjectValidator : AbstractValidator<ProjectViewModel>
    {
        public ProjectValidator()
        {
            RuleFor(h => h.Application).NotNull().WithName("Application");
            RuleFor(h => h.FundingType).NotNull().WithName("Funding Type");
            RuleFor(h => h.Description).NotEmpty().WithName("Description");
            RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
        }
    }
    
    [Validator(typeof(ProjectValidator))]
    public class ProjectViewModel
    {
        public string Application { get; set; }
        public string FundingType { get; set; }
        public string Description { get; set; }
        public string Name { get; set; }
    }
    
  4. Application_Start注册验证器时:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
        ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
    }
    
  5. 定义一个控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new ProjectViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(ProjectViewModel model)
        {
            return View(model);
        }
    }
    
  6. 还有一个观点:

    @model Appame.Models.ProjectViewModel
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.Application)
            @Html.EditorFor(x => x.Application)
            @Html.ValidationMessageFor(x => x.Application)
        </div>
        <div>
            @Html.LabelFor(x => x.FundingType)
            @Html.EditorFor(x => x.FundingType)
            @Html.ValidationMessageFor(x => x.FundingType)
        </div>
        <div>
            @Html.LabelFor(x => x.Description)
            @Html.EditorFor(x => x.Description)
            @Html.ValidationMessageFor(x => x.Description)
        </div>
        <div>
            @Html.LabelFor(x => x.Name)
            @Html.EditorFor(x => x.Name)
            @Html.ValidationMessageFor(x => x.Name)
        </div>
    
        <input type="submit" value="OK" />
    }
    
于 2011-03-21T16:56:42.973 回答