3
嗨我試圖在ASPNET MVC 4項目上實現IValidableObject接口。我的模型在不同的程序集中,當框架執行我的validate方法時,它顯示沒有validationContext並向我顯示此錯誤: validationContext無法通過嵌套訪問外部類型'AS.Core.Model.Horario.Validate'的非靜態成員鍵入'AS.Core.Model.Horario'獨立程序集中的IValidatableObject
很明顯,問題是關於單獨的程序集,但我不知道該怎麼做才能做到這一點,任何人都可以幫助我?
這是我的代碼:
public class Horario : IValidatableObject
{
[Range(0, 6, ErrorMessage = "Dia inválido")]
public byte Dia { get; set; }
[DataType(DataType.Time, ErrorMessage = "Horário de início inválido")]
public TimeSpan? HorarioInicio { get; set; }
[DataType(DataType.Time, ErrorMessage = "Horário fim inválido")]
public TimeSpan? HorarioFim { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (HorarioInicio != null && HorarioFim == null || HorarioFim != null && HorarioInicio == null)
{
yield return new ValidationResult("Horário inválido");
}
}
}