Пример #1
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <object> attributes, Type containerType, Type modelType, string propertyName)
        {
            var realTypeMetadata = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);
            var buddyType        = BuddyTypeAttribute.GetBuddyType(modelType);

            if (buddyType != null)
            {
                var buddyMetadata = base.CreateMetadataPrototype(attributes, containerType, buddyType, propertyName);
                foreach (var realProperty in realTypeMetadata.Properties)
                {
                    var buddyProperty = buddyMetadata.Properties.SingleOrDefault(bp => string.Equals(bp.PropertyName, realProperty.PropertyName, StringComparison.Ordinal));
                    if (buddyProperty != null)
                    {
                        // TODO: Only overwrite if the real type doesn't explicitly set it
                        realProperty.IsReadOnly          = buddyProperty.IsReadOnly;
                        realProperty.IsRequired          = buddyProperty.IsRequired;
                        realProperty.DisplayName         = buddyProperty.DisplayName;
                        realProperty.DisplayFormatString = buddyProperty.DisplayFormatString;
                        realProperty.SimpleDisplayText   = buddyProperty.SimpleDisplayText;
                        realProperty.DataTypeName        = buddyProperty.DataTypeName;
                        realProperty.Description         = buddyProperty.Description;
                        realProperty.EditFormatString    = buddyProperty.EditFormatString;
                        realProperty.NullDisplayText     = buddyProperty.NullDisplayText;
                        realProperty.ShowForDisplay      = buddyProperty.ShowForDisplay;
                        realProperty.ShowForEdit         = buddyProperty.ShowForEdit;
                        realProperty.TemplateHint        = buddyProperty.TemplateHint;
                    }
                }
            }

            return(realTypeMetadata);
        }
Пример #2
0
        protected override IEnumerable <IModelValidator> GetValidators(ModelMetadata metadata, IEnumerable <object> attributes)
        {
            var buddyType = BuddyTypeAttribute.GetBuddyType(metadata.ContainerType ?? metadata.ModelType);

            if (buddyType != null)
            {
                var buddyProperty = buddyType.GetTypeInfo().GetDeclaredProperty(metadata.PropertyName);
                if (buddyProperty != null)
                {
                    var buddyTypeAttributes = buddyProperty.GetCustomAttributes();
                    // TODO: De-dupe?
                    attributes = attributes.Concat(buddyTypeAttributes);
                    return(base.GetValidators(metadata, attributes));
                }
            }

            return(Enumerable.Empty <IModelValidator>());
        }