在MVC3应用程序中使用FluentValidation的错误消息

时间:2012-06-19 20:57:43

标签: c# asp.net asp.net-mvc-3 visual-studio-2010 fluentvalidation

我正在Visual Studio 2010中创建一个asp.net mvc 3应用程序。我试图对我的模型使用FluentValidation。我使用NuGet安装了软件包(FluentValidation,FluentValidationMVC3)。我的模型如下(本论坛简化):

public class ActivityLine : AbstractValidator<ActivityLine>
{
    [Key]
    [Required]
    public int ActivityLineId { get; set; }

    public virtual ICollection<TimeModel> TimeModels { get; set; }
}

public class ActivityLineValidator : AbstractValidator<ActivityLine>
{
    public ActivityLineValidator()
    {
        RuleFor(x => x.TimeModels).SetCollectionValidator(new TimeModelValidator());
    }
}


[Validator(typeof(TimeModelValidator))]
public class TimeModel
{
    [Key]
    public int TimeModelId { get; set; }
    [Required]
    public int ActivityLineId { get; set; }
    [Required]
    public int LineNumber { get; set; }
    public int startHours { get; set; }
    public int startMinutes { get; set; }
    public int endHours { get; set; }
    public int endMinutes { get; set; }

    public virtual ActivityLine ActivityLine { get; set; }

    [NotMapped]
    public TimeSpan StartTime
    {
        get { return new TimeSpan(startHours, startMinutes, 0); }
        set { startHours = value.Hours; startMinutes = value.Minutes; }
    }
    [NotMapped]
    public TimeSpan EndTime 
    {
        get { return new TimeSpan(endHours, endMinutes, 0); }
        set { endHours = value.Hours; endMinutes = value.Minutes; } 
    }
}

public class TimeModelValidator : AbstractValidator<TimeModel>
{
    public TimeModelValidator()
    {
        RuleFor(timemodel => timemodel.endHours)
           .GreaterThan(timemodel => timemodel.startHours)
           .WithMessage("Start Time Must be before End Time");
    }
}

在我的控制器中:

[HttpPost]
public ActionResult Create(ActivityLine model)
{
    if (ModelState.IsValid)
    {
        personnel.ActivityLines.Add(model);
        personnel.SaveChanges();
        return RedirectToAction("Index");
    }
    //Model State Not Valid Redisplay Form
    return View(model);
}

即使我为TimeModel和TimeModel实例分配了无效值,ModelState也始终为true。当项目击中人员时,该项目就会爆炸.SaveChagnes()行。我收到“No Source Available”错误。详情如下:

Locating source for 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs'. Checksum: MD5 {e3 d4 90 62 70 f7 a6 4e 9e ac 62 71 77 21 64 dd}

文件'c:\ Projects \ FluentValidation \ src \ FluentValidation \ Attributes \ ValidatorAttribute.cs'不存在。 查看'c:\ Projects \ FluentValidation \ src \ FluentValidation \ Attributes \ ValidatorAttribute.cs'的脚本文档... 查看'c:\ Projects \ FluentValidation \ src \ FluentValidation \ Attributes \ ValidatorAttribute.cs'的项目。 在项目中找不到该文件。 查看目录'C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ crt \ src \'... 查看目录'C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ mfc \'... 查看目录'C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ atl \'... 查看目录'C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ include \'... 活动解决方案的调试源文件设置表明调试器不会要求用户查找文件:c:\ Projects \ FluentValidation \ src \ FluentValidation \ Attributes \ ValidatorAttribute.cs。 调试器无法找到源文件'c:\ Projects \ FluentValidation \ src \ FluentValidation \ Attributes \ ValidatorAttribute.cs'。

我认为我的配置有问题,但我无法弄清楚是什么。有没有人使用Fluent有这个问题?我需要做些什么来解决它?我的项目位于网络共享上,如果这会产生影响。

1 个答案:

答案 0 :(得分:1)

您的ActivityLine域模型不应派生自AbstractValidator。它应该使用Validator属性进行修饰,就像使用TimeModel

一样
[Validator(typeof(ActivityLineValidator))]
public class ActivityLine
{
    [Key]
    [Required]
    public int ActivityLineId { get; set; }

    public virtual ICollection<TimeModel> TimeModels { get; set; }
}

public class ActivityLineValidator : AbstractValidator<ActivityLine>
{
    public ActivityLineValidator()
    {
        RuleFor(x => x.TimeModels).SetCollectionValidator(new TimeModelValidator());
    }
}

另外,请确保Application_Start中有以下行,以启用FluentValidation元数据提供程序,如documentation中所述:

FluentValidationModelValidatorProvider.Configure();

personnel.SaveChagnes()行的轰炸而言,这与ASP.NET MVC和FluentValidation.NET都没有任何关系。我想这是你正在使用的数据访问技术,你遇到了问题。