user137348
user137348

Reputation: 10332

ASP.NET MVC Custom MetadataProvider

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

Answers (1)

murki
murki

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

Related Questions