【问题标题】:Extending CachedDataAnnotationsModelMetadataProvider to not cache some DataAnnotations扩展 CachedDataAnnotationsModelMetadataProvider 以不缓存某些 DataAnnotations
【发布时间】:2015-03-18 19:06:41
【问题描述】:

我正在尝试扩展 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&lt;TModelMetadata&gt;方法

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

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

【问题讨论】:

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


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2021-10-25
      • 2012-12-22
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多