【问题标题】:Problem getting started with FluentValidationFluentValidation 入门问题
【发布时间】:2015-05-28 21:35:08
【问题描述】:

我正在尝试将 FluentValidation 2.0 与 MVC 3 项目一起使用。我已按照here 的说明在项目中安装 FV。

这是我的验证器类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentValidation;

namespace HandicapTracker.Models.Validation
{
    public class TournamentValidator : AbstractValidator<Tournament>
    {
        public TournamentValidator()
        {
            RuleFor(x => x.CourseId).NotEmpty();
            RuleFor(x => x.TournamentDate).NotEmpty().NotEqual(new DateTime());
        }
    }
}

这是我尝试使用该属性的地方:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using HandicapTracker.Configuration;
using HandicapTracker.Models.Validation;
using Omu.ValueInjecter;
using FluentValidation;
using HandicapTracker.Models.Validation;

namespace HandicapTracker.Models
{
    [Validator(typeof(TournamentValidator))]
    public class Tournament : HandicapTrackerModelBase
    {
        private const int VisitingTeamIndex = 0;
        private const int HomeTeamIndex = 1;

        public Tournament() : this (new League())
        {

        }
        ....
}

但是,无法识别该属性。当我构建时,我收到以下错误消息:

“System.ComponentModel.DataAnnotations.Validator”不是属性类。

我实际上已经在两种不同的解决方案上进行了尝试,并且在这两种解决方案上都遇到了同样的问题。这可能是微不足道的事情,但我无法弄清楚。

谁能告诉我我做错了什么?

谢谢。

【问题讨论】:

    标签: asp.net-mvc-3 fluentvalidation


    【解决方案1】:

    看起来您的[Validator] 属性正在使用System.ComponentModel.DataAnnotations 命名空间中另一个名为Validator 的类。尝试完全限定属性。

    [FluentValidation.Attributes.Validator(typeof(TournamentValidator))]
    

    否则,请修改您的 using 语句以避免验证器名称冲突。

    【讨论】:

      【解决方案2】:

      不要直接使用FluentVallidation命名空间,它应该从servicestack实现。所以你可以写成

      using Servicestack.FluentValidation;
      

      【讨论】:

        猜你喜欢
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多