Reputation: 10332
I'm building a custom MetadataProvider and I'd like to access the actuall model value in the CreateMetadata
method.
public class IcpMetadataProvider : AssociatedMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = new ModelMetadata(this, containerType, modelAccessor, modelType, propertyName);
//Is null when the model is of reference type
var model = metadata.Model;
return metadata;
}
}
When the current model value is of type string, the model value can be found in metadata.Model
. But when the model is of reference type the value is null.
Or maybe is there any way to pass custom data to this method ?
Upvotes: 2
Views: 1810
Reputation: 879
Quoting Brad Wilson, you need to "Call base.CreateMetadata so that you can get the ModelMetadata that’s filled with values from DataAnnotations, and then just supplement it with values from your own attributes."
Ref link: http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html
Upvotes: 1