开发者

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 refere开发者_运维问答nce 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 ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜