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.NETMVC3の最新バージョンで機能する手順は次のとおりです。

  1. デフォルトのVisualStudioテンプレートを使用して、新しいASP.NETMVC3プロジェクトを作成します。
  2. FluentValidation.MVC3NuGetパッケージをインストールします。
  3. ビューモデルとそれに対応するバリデーターを追加します(この場合、プロジェクトタイプのバリデーターがありますが、一貫性のない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 に答える