asp.net-mvc-3 - FluentValidation入门问题

标签 asp.net-mvc-3 fluentvalidation

我正在尝试将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”不是属性类。

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

有人可以告诉我我做错了吗?

谢谢。

最佳答案

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

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

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

关于asp.net-mvc-3 - FluentValidation入门问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5346059/

相关文章:

javascript - 在 MVC 应用程序中组织 JS 代码

javascript - 如何上传文件到服务器?

c# - 使用 FluentValidation 在 Blazor 中进行自定义验证

servicestack - 为什么ServiceStack没有实现Validate方法中的ruleSet可选参数?

c# - Fluent Validation 将 CustomAsync 更改为 MustAsync

c# - FluentValidation 正在请求 IValidator<string>

asp.net-mvc-3 - 坚持使用 ASPNETCOMPILER : error ASPRUNTIME: Type is not resolved for member 'FluentNHibernate. Cfg.FluentConfigurationException,FluentNHibernate

javascript - 如何重新排列 KendoGrid Tab 顺序?

c# - 如何在类库项目中使用 TagBuilder?

c# - FluentValidation 可以处理嵌套集合吗?