2015-03-03 62 views
0

我試圖擴展CachedDataAnnotationsModelMetadataProvider不緩存自定義ValidationAttribute。 我怎樣才能做到這一點?我嘗試着在aspnet中查找,但是得到答案太複雜了;我是否需要重寫,作爲保護覆蓋擴展CachedDataAnnotationsModelMetadataProvider不緩存一些DataAnnotations

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) 

是密封的。有任何想法嗎 ?

回答

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