I would like to write my own model binder for DateTime
type. First of all I'd like to write a new attribute that I can attach to my model property like:
[DateTimeFormat("d.M.yyyy")]
public DateTime Birth { get; set,}
This is the easy part. But the binder part is a bit more difficult. I would like to add a new model binder for type DateTime
. I can either
IModelBinder
interface and write my own BindModel()
methodDefaultModelBinder
and override BindModel()
methodMy model has a property as seen above (Birth
). So when the model tries to bind request data to this property, my model binder's BindModel(controllerContext, bindingContext)
gets invoked. Everything ok, but. How do I get property attributes from controller/bindingContext, to parse my date correctly? How can I get to the PropertyDesciptor
of property Birth
?
Because of separation of concerns my model class is defined in an assembly that doesn't (and shouldn't) reference System.Web.MVC assembly. Setting custom binding (similar to Scott Hanselman's example) attributes is a no-go here.
I don't think you should put locale-specific attributes on a model.
Two other possible solutions to this problem are:
To answer your actual question, the way to get custom attributes (for MVC 2) is to write an AssociatedMetadataProvider.