Configure Custom MetadataProvider for ViewModel

不打扰是莪最后的温柔 提交于 2019-12-07 23:44:30

I'm pretty sure changing the current model metadata provider in the ViewModel is not safe once you start getting multiple users on the site, let alone thread safe. You might be able to use the attribute method but you'll still have to implement your own ModelMetadataProvider and set it to Current at the start of your app, then inspect for certain attributes and determine your ModelMetaData to return, if there are none then fall through to the base implementation. Though to be honest, the amount of restrictions that you're talking about, having it only handle selected view models but not being allowed to know or test for those view models? It sounds like you're doing something wrong elsewhere...

UPDATE: When I needed a ModelMetadata provider I created one that looks something like this...

public class MyMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        if ((containerType != typeof(MyType))
            return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        //setup custom ModelMetadata here
    }
}

You can't.

However, you can act as a proxy for all other models. Something like:

public class YourProvider<TViewModel>
{
    public YourProvider(InnerProvider provider) {}


    public ModelMetaData GetMetaData(SomeContext context)
    {   
        if (context.ModelType != typeof(TViewModel))
            return _innerProvider.GetMetaData(context);

        //Other logic here.

    }
}

And finally assign it as:

ModelMetadataProviders.Current 
    = new MyModelMetadataProvider<CustomViewModel>(ModelMetadataProviders.Current);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!