3

Hi I'm trying to implement IValidableObject interface on a ASPNET MVC 4 project. My model is in a different assembly, when the framework executes my validate method it shows no validationContext and shows me this error: validationContext Cannot access a non-static member of outer type 'AS.Core.Model.Horario.Validate' via nested type 'AS.Core.Model.Horario'

It's clear that the problem is about separate assemblies but I have no idea what to do to make this work, anybody could help me?

This is my code:

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");
        }
    }
}
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Eduardo Cucharro
  • 525
  • 1
  • 4
  • 14

1 Answers1

0

You need to write ViewModel that will be IValidatableObject and move data from it to your business model in controller after validation.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38