扩展CachedDataAnnotationsModelMetadataProvider以不缓存某些DataAnnotations

时间:2015-03-03 14:04:03

标签: asp.net asp.net-mvc-4 metadata modelmetadata

我正在尝试将CachedDataAnnotationsModelMetadataProvider扩展为不缓存自定义ValidationAttribute。 我怎样才能实现这一目标?我试着查看aspnetwebstack,但它太复杂了,无法得到答案;我需要覆盖什么,作为受保护的覆盖

protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype,
            Func<object> modelAccessor)

protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(
            IEnumerable<Attribute> attributes,
            Type containerType,
            Type modelType,
            string propertyName)

CachedAssociatedMetadataProvider<TModelMetadata>方法

protected sealed override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)

是密封的。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我的问题是由于构造函数中的属性初始化而未更新客户端验证。所以我将属性加载到了

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    var userSettings = ServiceLocator.Resolve<UserSettings>();
    if (userSettings != null)
    {
        // this was the required field to update
        this.StringLengthAttribute.MinimumLength = userSettings.MinRequiredPasswordLength;
    }

    yield return new ModelClientValidationStringLengthRule(this.ErrorMessage, this.StringLengthAttribute.MinimumLength, this.StringLengthAttribute.MaximumLength);
}