I have a model called Foo which has a property called MyProp of type Bar.
When I post this model to the controller I want the model binder to validate MyProp because it has the Required attribute just as it does with a string. I need this to be self-contained within the Bar class or as a separate class. I have tried to use the IValidatableObject on the Bar class but it seems like it’s impossible to check if the Foo class has the Required attribute on MyProp? So now I’m out of options and need some help. Below is some sample code for my question.
public class Foo {
[Required]
public Bar MyProp { get; set; }
}
public class Bar {
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
}
Here is one solution to my problem where I can use the built in required attribute and still get custom behavior. This is just some proof of concept code.
The model:
The model binder:
The model binder provider:
And last the global.asax registration:
The result: http://cl.ly/IjCS
So what do you think about this solution?