protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); PropertyEditableAttribute editable = attributes.OfType <PropertyEditableAttribute>().FirstOrDefault(); if (editable == null) { editable = PropertyEditableAttribute.Default; } metadata.AdditionalValues.Add(EditableWhenCreate_Key, editable.WhenCreate); metadata.AdditionalValues.Add(EditableWhenEdit_Key, editable.WhenEdit); metadata.AdditionalValues.Add(Icon_Prefix, attributes.OfType <IconAttribute>().Select(i => i.File).FirstOrDefault()); metadata.AdditionalValues.Add(RealSort_Key, attributes.OfType <RealSortAttribute>().Select(s => s.Expression).FirstOrDefault()); TypeConverterAttribute typeConverterAttribute = attributes.OfType <TypeConverterAttribute>().FirstOrDefault(); if (typeConverterAttribute != null && typeConverterAttribute.ConverterTypeName != null) { metadata.AdditionalValues.Add(ConverterTypeName_Key, typeConverterAttribute.ConverterTypeName); } foreach (var item in attributes.OfType <KeyValueInfoAttribute>()) { metadata.AdditionalValues.Add(KeyValueInfoAttribute_Prefix + item.Key, item.Value); } DataFormatAttribute dataFormatAttribute = attributes.OfType <DataFormatAttribute>().FirstOrDefault(); if (dataFormatAttribute != null) { metadata.EditFormatString = dataFormatAttribute.EditFormatStringWithBraces; metadata.DisplayFormatString = dataFormatAttribute.DiaplayFormatStringWithBraces; metadata.NullDisplayText = dataFormatAttribute.NullDisplayText; metadata.ConvertEmptyStringToNull = dataFormatAttribute.ConvertEmptyStringToNull; } EnumerationAttribute enumerationAttribute = attributes.OfType <EnumerationAttribute>().FirstOrDefault(); if (enumerationAttribute != null) { metadata.AdditionalValues.Add(EnumerationAttribute_Key, enumerationAttribute); } BoolAttribute boolAttribute = attributes.OfType <BoolAttribute>().FirstOrDefault(); if (boolAttribute != null) { metadata.AdditionalValues.Add(BoolAttribute_Key, boolAttribute); } metadata.AdditionalValues.Add(AllAttribute_Key, attributes); return(metadata); }
protected override ModelMetadata CreateMetadata(System.Collections.Generic.IEnumerable <System.Attribute> attributes, System.Type containerType, System.Func <object> modelAccessor, System.Type modelType, string propertyName) { ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); PropertyEditableAttribute editable = attributes.OfType <PropertyEditableAttribute>().FirstOrDefault <PropertyEditableAttribute>(); if (editable == null) { editable = PropertyEditableAttribute.Default; } metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.EditableWhenCreate_Key, editable.WhenCreate); metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.EditableWhenEdit_Key, editable.WhenEdit); metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.Icon_Prefix, ( from i in attributes.OfType <IconAttribute>() select i.File).FirstOrDefault <string>()); metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.RealSort_Key, ( from s in attributes.OfType <RealSortAttribute>() select s.Expression).FirstOrDefault <string>()); TypeConverterAttribute typeConverterAttribute = attributes.OfType <TypeConverterAttribute>().FirstOrDefault <TypeConverterAttribute>(); if (typeConverterAttribute != null && typeConverterAttribute.ConverterTypeName != null) { metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.ConverterTypeName_Key, typeConverterAttribute.ConverterTypeName); } foreach (KeyValueInfoAttribute item in attributes.OfType <KeyValueInfoAttribute>()) { metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.KeyValueInfoAttribute_Prefix + item.Key, item.Value); } DataFormatAttribute dataFormatAttribute = attributes.OfType <DataFormatAttribute>().FirstOrDefault <DataFormatAttribute>(); if (dataFormatAttribute != null) { metadata.EditFormatString = dataFormatAttribute.EditFormatStringWithBraces; metadata.DisplayFormatString = dataFormatAttribute.DiaplayFormatStringWithBraces; metadata.NullDisplayText = dataFormatAttribute.NullDisplayText; metadata.ConvertEmptyStringToNull = dataFormatAttribute.ConvertEmptyStringToNull; } return(metadata); }