0

我正在Visual Studio 2010中創建一個asp.net mvc 3應用程序。我嘗試將FluentValidation用於我的模型。我使用NuGet安裝了包(FluentValidation,FluentValidationMVC3)。我的模型如下(簡化了這個論壇):錯誤消息在MVC3應用程序中使用FluentValidation

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); 
} 

的ModelState始終是真實的,甚至當我指定無效值和實例TimeModel的。然後該項目在碰到人員時彈出.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:\項目\ FluentValidation \ SRC \ FluentValidation \屬性\ 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

您的ActivityLine域模型不應該從AbstractValidator派生。它應與驗證屬性被裝飾爲你和你的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以下行,以便能夠在documentation作爲解釋FluentValidation元數據提供:

FluentValidationModelValidatorProvider.Configure(); 

personnel.SaveChagnes()線上的轟炸而言,這與ASP.NET MVC和FluentValidation.NET都無關。我想這是您正在使用的數據訪問技術,您遇到了問題。

+0

感謝您的快速響應。我真不敢相信我錯過了。我想我剛剛盯着代碼太久了。 – FrstCBC

相關問題