I have this ViewModel (simplified):
public class ResponseViewModel {
public QuestionViewModel Question { get; set; }
public string Answer { get; set; }
}
public class QuestionViewModel {
public string Text { get; set; }
public string Description { get; set; }
public bool IsRequired { get; set; }
}
QuestionViewModel is mapped from my DAL entity Question which is a straightforward mapping from:
public class Question {
public int Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public bool IsRequired { get; set; }
}
I want to be able to make Answer
Required if Question.IsRequired
is true.
However after the postback Only The property Answer
is filled (of course).
What is the best way to go here? I would like to be able to create a validation attribute but don't know how to achieve this.
UPDATE:
I tried to make it work by using ModelBinding but until now no succes. What I have done:
public class EntityModelBinder : DefaultModelBinder
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
// IF I DO IT HERE I AM TOO EARLY
}
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
base.OnModelUpdated(controllerContext, bindingContext);
// IF I DO IT HERE I AM TOO LATE. VALIDATION ALREADY TOOK PLACE
}
}