加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 22:23:33 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将FluentValidation 2.0与MVC 3项目一起使用.我已按照说明 here在项目中安装FV. 这是我的验证器类: using System;using System.Collections.Generic;using System.Linq;using System.Web;using FluentValidation;namespace HandicapTracker.Model
我正在尝试将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]属性正在拾取System.ComponentModel.DataAnnotations命名空间中另一个名为Validator的类.尝试完全限定属性.
[FluentValidation.Attributes.Validator(typeof(TournamentValidator))]

否则,修改using语句以避免Validator名称冲突.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读