2011-03-17 88 views
8

我正在尝试在MVC 3项目中使用FluentValidation 2.0。我已按照指示here在项目中安装FV。FluentValidation问题入门

这是我的验证器类:

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”不是属性类。

我已经尝试了两种不同的解决方案,并且两者都有相同的问题。这可能是微不足道的,但我无法弄清楚。

有人能告诉我我做错了什么吗?

谢谢。

回答

24

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

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

否则,请修改您的使用语句以避免验证程序名称冲突。

0

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

using Servicestack.FluentValidation;