2

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 ?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.