private ModelMetadata CloneMetadata(Func <object> modelAccessor)
        {
            var           cachedMetadata      = _metadata as CachedDataAnnotationsModelMetadata;
            var           annotationsMetadata = _metadata as DataAnnotationsModelMetadata;
            ModelMetadata clonedMetadata;

            if (cachedMetadata != null)
            {
                clonedMetadata = new CachedDataAnnotationsModelMetadata(cachedMetadata, modelAccessor);
            }
            else if (annotationsMetadata != null)
            {
                var provider = (_oldMetadataProvider as DataAnnotationsModelMetadataProvider) ?? AnnotationsProvider;
                clonedMetadata = new DataAnnotationsModelMetadata(
                    provider,
                    annotationsMetadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    annotationsMetadata.PropertyName,
                    displayColumnAttribute: null);      // Copying SimpleDisplayText below compensates for null here.
            }
            else
            {
                clonedMetadata = new ModelMetadata(
                    _oldMetadataProvider,
                    _metadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    _metadata.PropertyName);
            }

            // Undo all the lazy-initialization of ModelMetadata and CachedDataAnnotationsModelMetadata...
            clonedMetadata.Container = _metadata.Container;        // May be incorrect.
            clonedMetadata.ConvertEmptyStringToNull = _metadata.ConvertEmptyStringToNull;
            clonedMetadata.DataTypeName             = _metadata.DataTypeName;
            clonedMetadata.Description             = _metadata.Description;
            clonedMetadata.DisplayFormatString     = _metadata.DisplayFormatString;
            clonedMetadata.DisplayName             = _metadata.DisplayName;
            clonedMetadata.EditFormatString        = _metadata.EditFormatString;
            clonedMetadata.HasNonDefaultEditFormat = _metadata.HasNonDefaultEditFormat;
            clonedMetadata.HideSurroundingHtml     = _metadata.HideSurroundingHtml;
            clonedMetadata.HtmlEncode               = _metadata.HtmlEncode;
            clonedMetadata.IsReadOnly               = _metadata.IsReadOnly;
            clonedMetadata.IsRequired               = _metadata.IsRequired;
            clonedMetadata.NullDisplayText          = _metadata.NullDisplayText;
            clonedMetadata.Order                    = _metadata.Order;
            clonedMetadata.RequestValidationEnabled = _metadata.RequestValidationEnabled;
            clonedMetadata.ShortDisplayName         = _metadata.ShortDisplayName;
            clonedMetadata.ShowForDisplay           = _metadata.ShowForDisplay;
            clonedMetadata.ShowForEdit              = _metadata.ShowForEdit;
            clonedMetadata.SimpleDisplayText        = _metadata.SimpleDisplayText;
            clonedMetadata.TemplateHint             = _metadata.TemplateHint;
            clonedMetadata.Watermark                = _metadata.Watermark;
            foreach (var keyValuePair in _metadata.AdditionalValues)
            {
                clonedMetadata.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            return(clonedMetadata);
        }
Exemplo n.º 2
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            modelMetadata.DisplayName = prototype.DisplayName;
            return(modelMetadata);
        }
 public CachedDataAnnotationsModelMetadata2(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
     : base(prototype, modelAccessor)
 {
     var prototype2 = prototype as CachedDataAnnotationsModelMetadata2;
     if (prototype2 != null)
     {
         this.Attributes = prototype2.Attributes;
     }
 }
Exemplo n.º 4
0
        public CachedDataAnnotationsModelMetadata2(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
            : base(prototype, modelAccessor)
        {
            var prototype2 = prototype as CachedDataAnnotationsModelMetadata2;

            if (prototype2 != null)
            {
                this.Attributes = prototype2.Attributes;
            }
        }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var metadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            var type = metadata.ModelType;
            if (type.IsEnum ||
                (type.IsGenericType(typeof(Nullable<>)) &&
                type.GetGenericArguments().Length == 1 && type.GetGenericArguments()[0].IsEnum))
                metadata.TemplateHint = "Enum";

            return metadata;
        }
Exemplo n.º 6
0
 /// <summary>
 /// If the model is a <see cref="Models.ActionExecutionModel"/>,
 /// return combined properties and parameters metadata, else fallback to default.
 /// </summary>
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
     CachedDataAnnotationsModelMetadata prototype,
     Func <object> modelAccessor)
 {
     if (typeof(Models.ActionExecutionModel).IsAssignableFrom(prototype.ModelType))
     {
         return(new ActionExecutionMetadata(this, prototype, modelAccessor));
     }
     else
     {
         return(base.CreateMetadataFromPrototype(prototype, modelAccessor));
     }
 }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var metadataFromPrototype = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            foreach (var keyValuePair in prototype.AdditionalValues)
            {
                metadataFromPrototype.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            // we need to preserve DisplayName fetched during prototype creation
            metadataFromPrototype.DisplayName = prototype.DisplayName;

            return metadataFromPrototype;
        }
Exemplo n.º 8
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <Attribute> attributes, Type containerType, Type modelType, string propertyName)
        {
            CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);

            if (string.IsNullOrEmpty(modelMetadata.DisplayName))
            {
                DisplayTextAttribute displayTextAttribute = attributes.OfType <DisplayTextAttribute>().FirstOrDefault();
                if (null != displayTextAttribute)
                {
                    displayTextAttribute.SetDisplayName(modelMetadata);
                }
            }
            return(modelMetadata);
        }
Exemplo n.º 9
0
        public ActionExecutionMetadata(DomosMetadataProvider provider,
                                       CachedDataAnnotationsModelMetadata prototype,
                                       Func <object> modelAccessor)
            : base(prototype, modelAccessor)
        {
            if (prototype == null)
            {
                throw new ArgumentNullException(nameof(prototype));
            }
            if (modelAccessor == null)
            {
                throw new ArgumentNullException(
                          nameof(modelAccessor),
                          "Metadata can only be extracted if a concrete model exists with ActionCodeName set. " +
                          "Use [Try]UpdateModelXXX methods on already created instances.");
            }

            var model = (Models.ActionExecutionModel)modelAccessor();

            var parameterSpecifications = model.GetParameterSpecifications();

            properties = new List <ModelMetadata>(parameterSpecifications.Count + prototype.Properties.Count());

            // Add all the original properties except "Parameters".
            properties.AddRange(prototype.Properties.Where(p => p.PropertyName != nameof(Models.ActionExecutionModel.Parameters)));

            foreach (var parameterSpecification in parameterSpecifications.Values)
            {
                object parameterValue = GetParameterValue(model, parameterSpecification);

                var property = new CachedDataAnnotationsModelMetadata(
                    provider,
                    typeof(Models.ActionExecutionModel),
                    parameterSpecification.Type,
                    parameterSpecification.Key,
                    parameterSpecification.ValidationAttributes)
                {
                    IsReadOnly  = false,
                    IsRequired  = parameterSpecification.IsRequired,
                    Container   = model,
                    Model       = parameterValue,
                    DisplayName = parameterSpecification.Caption,
                    Description = parameterSpecification.Description,
                };

                properties.Add(property);

                this.AdditionalValues[parameterSpecification.Key] = parameterValue;
            }
        }
Exemplo n.º 10
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var metadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            var type     = metadata.ModelType;

            if (type.IsEnum ||
                (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                 type.GetGenericArguments().Length == 1 && type.GetGenericArguments()[0].IsEnum))
            {
                metadata.TemplateHint = "Enum";
            }
            return(metadata);
        }
		protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
		{
			var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);

			if (prototype.AdditionalValues.Count > 0)
			{
				var attrs = prototype.AdditionalValues.Where(x => x.Value is IModelAttribute).ToArray();
				if (attrs.Any())
				{
					result.AdditionalValues.AddRange(attrs);
				}
			}

			return result;		
		}
Exemplo n.º 12
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var modelMetaData = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            Composite composite;
            if (this.compositeByModelType.TryGetValue(modelMetaData.ModelType, out composite))
            {
                // CompositeType
                modelMetaData.SetComposite(composite);

                foreach (var filter in this.typeAnnotations)
                {
                    filter.OnTypeMetadataCreated(modelMetaData);
                }
            }
            else if (modelMetaData.ContainerType != null && this.compositeByModelType.TryGetValue(modelMetaData.ContainerType, out composite))
            {
                modelMetaData.SetComposite(composite);

                // Path
                if (composite != null)
                {
                    Dictionary<string, Path> pathByPropertyName;
                    if (this.pathByPropertyNameByModelType.TryGetValue(modelMetaData.ContainerType, out pathByPropertyName))
                    {
                        Path path;
                        if (pathByPropertyName.TryGetValue(modelMetaData.PropertyName, out path))
                        {
                            modelMetaData.SetPath(path);
                        }
                    }
                }

                foreach (var filter in this.propertyAnnotations)
                {
                    filter.OnPropertyMetadataCreated(modelMetaData);
                }
            }

            return modelMetaData;
        }
        public void AttributesOverrideMetadataStrings(Attribute attribute, Func<ModelMetadata, string> accessor)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(ClassWithDisplayableColumn),
                propertyName: null,
                attributes: attributes)
            {
                Model = new ClassWithDisplayableColumn { Property = "value" },
            };
            var result = accessor(metadata);

            // Assert
            Assert.Equal("value", result);
        }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            object objectAttributes;

            if (prototype.AdditionalValues.TryGetValue(DesensitizionKey.DesensitizionAttribute, out objectAttributes))
            {
                IList <DesensitizationAttribute> desensitizationAttributes = objectAttributes as IList <DesensitizationAttribute>;
                IList <DesensitizationAttribute> matchedAttributes         = new List <DesensitizationAttribute>();

                if (desensitizationAttributes == null || desensitizationAttributes.Count() <= 0)
                {
                    return(modelMetadata);
                }

                foreach (var desensitizationAttribute in desensitizationAttributes)
                {
                    var propertyFullName = $"{modelMetadata.ContainerType.FullName}.{modelMetadata.PropertyName}";
                    if (!string.IsNullOrEmpty(desensitizationAttribute.ShortDisplayName))
                    {
                        propertyFullName += $".{desensitizationAttribute.ShortDisplayName}";
                    }
                    var showForDisplay = desensitizationAttribute.RuleName.IsHasDisplayPermission(propertyFullName);
                    if (!showForDisplay)
                    {
                        matchedAttributes.Add(desensitizationAttribute);
                    }
                }

                modelMetadata.AdditionalValues.Add(DesensitizionKey.DesensitizionAttribute, matchedAttributes);
            }
            if (prototype.AdditionalValues.ContainsKey(DesensitizionKey.CustomMethodAttribute))
            {
                modelMetadata.AdditionalValues.Add(DesensitizionKey.CustomMethodAttribute,
                                                   prototype.AdditionalValues[DesensitizionKey.CustomMethodAttribute]);
            }
            return(modelMetadata);
        }
        public void Constructor_DefersDefaultsToBaseModelMetadata()
        {
            // Arrange
            var attributes = Enumerable.Empty<Attribute>();
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(object),
                propertyName: null,
                attributes: attributes);

            // Assert
            Assert.True(metadata.ConvertEmptyStringToNull);
            Assert.False(metadata.IsReadOnly);
            Assert.False(metadata.IsRequired);

            Assert.Null(metadata.Description);
            Assert.Null(metadata.DisplayName);
            Assert.Null(metadata.NullDisplayText);
        }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <Attribute> attributes, Type containerType, Type modelType, string propertyName)
        {
            CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);

            var customMethodAttributes = attributes.OfType <CustomMethodAttribute>().ToList();

            if (customMethodAttributes != null && customMethodAttributes.Count() > 0)
            {
                modelMetadata.AdditionalValues.Add(DesensitizionKey.CustomMethodAttribute, customMethodAttributes);
            }

            //如果是类型不做处理
            if (modelMetadata.ContainerType == null)
            {
                return(modelMetadata);
            }

            var propertyFullName = $"{modelMetadata.ContainerType.FullName}.{modelMetadata.PropertyName}";
            List <DesensitizationAttribute> matchedAttributes = new List <DesensitizationAttribute>();

            if (DesensitizationRuleDictionary.Rules.ContainsKey(propertyFullName))
            {
                var desensitizationAttribute = DesensitizationRuleDictionary.Rules[propertyFullName];
                matchedAttributes.AddRange(desensitizationAttribute);
            }

            var desensitizationAttributes = attributes.OfType <DesensitizationAttribute>().ToList();

            if (desensitizationAttributes != null && desensitizationAttributes.Count() > 0)
            {
                matchedAttributes.AddRange(desensitizationAttributes);
            }

            modelMetadata.AdditionalValues.Add(DesensitizionKey.DesensitizionAttribute, matchedAttributes);
            return(modelMetadata);
        }
Exemplo n.º 17
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            if (prototype.AdditionalValues.Count > 0)
            {
                var attrs = prototype.AdditionalValues.Where(x => x.Value is IModelAttribute).ToArray();
                if (attrs.Any())
                {
                    result.AdditionalValues.AddRange(attrs);
                }
            }

            return(result);
        }
Exemplo n.º 18
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var metadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            metadata.ConvertEmptyStringToNull = false;
            return(metadata);
        }
        private ModelMetadata CloneMetadata(Func<object> modelAccessor)
        {
            var cachedMetadata = _metadata as CachedDataAnnotationsModelMetadata;
            var annotationsMetadata = _metadata as DataAnnotationsModelMetadata;
            ModelMetadata clonedMetadata;
            if (cachedMetadata != null)
            {
                clonedMetadata = new CachedDataAnnotationsModelMetadata(cachedMetadata, modelAccessor);
            }
            else if (annotationsMetadata != null)
            {
                var provider = (_oldMetadataProvider as DataAnnotationsModelMetadataProvider) ?? AnnotationsProvider;
                clonedMetadata = new DataAnnotationsModelMetadata(
                    provider,
                    annotationsMetadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    annotationsMetadata.PropertyName,
                    displayColumnAttribute: null);      // Copying SimpleDisplayText below compensates for null here.
            }
            else
            {
                clonedMetadata = new ModelMetadata(
                    _oldMetadataProvider,
                    _metadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    _metadata.PropertyName);
            }

            // Undo all the lazy-initialization of ModelMetadata and CachedDataAnnotationsModelMetadata...
            clonedMetadata.Container = _metadata.Container;        // May be incorrect.
            clonedMetadata.ConvertEmptyStringToNull = _metadata.ConvertEmptyStringToNull;
            clonedMetadata.DataTypeName = _metadata.DataTypeName;
            clonedMetadata.Description = _metadata.Description;
            clonedMetadata.DisplayFormatString = _metadata.DisplayFormatString;
            clonedMetadata.DisplayName = _metadata.DisplayName;
            clonedMetadata.EditFormatString = _metadata.EditFormatString;
            clonedMetadata.HasNonDefaultEditFormat = _metadata.HasNonDefaultEditFormat;
            clonedMetadata.HideSurroundingHtml = _metadata.HideSurroundingHtml;
            clonedMetadata.HtmlEncode = _metadata.HtmlEncode;
            clonedMetadata.IsReadOnly = _metadata.IsReadOnly;
            clonedMetadata.IsRequired = _metadata.IsRequired;
            clonedMetadata.NullDisplayText = _metadata.NullDisplayText;
            clonedMetadata.Order = _metadata.Order;
            clonedMetadata.RequestValidationEnabled = _metadata.RequestValidationEnabled;
            clonedMetadata.ShortDisplayName = _metadata.ShortDisplayName;
            clonedMetadata.ShowForDisplay = _metadata.ShowForDisplay;
            clonedMetadata.ShowForEdit = _metadata.ShowForEdit;
            clonedMetadata.SimpleDisplayText = _metadata.SimpleDisplayText;
            clonedMetadata.TemplateHint = _metadata.TemplateHint;
            clonedMetadata.Watermark = _metadata.Watermark;
            foreach (var keyValuePair in _metadata.AdditionalValues)
            {
                clonedMetadata.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            return clonedMetadata;
        }
        /// <summary>
        /// Creates the metadata prototype.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <param name="containerType">Type of the container.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>metadata prototype</returns>
        protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <Attribute> attributes, Type containerType, Type modelType, string propertyName)
        {
            var attrList = attributes.ToList();
            CachedDataAnnotationsModelMetadata prototype = base.CreateMetadataPrototype(attrList, containerType, modelType, propertyName);

            //Add custom prototype data, e.g.
            prototype.AdditionalValues.Add("Attributes", attributes);

            //TODO: can any of this griddy stuff be done in the prototype
            //if (!string.IsNullOrEmpty(propertyName) && (containerType == null || (Nullable.GetUnderlyingType(containerType) == null)))
            //{
            //    // Get parent model to be able to retrieve dependent property values
            //    var parentModel = GetParentModel(modelAccessor);

            //    if (parentModel != null)
            //    {
            //        prototype.AdditionalValues.Add("ParentModel", parentModel);
            //        prototype.AdditionalValues.Add("PropertyNameInParentModel", propertyName);

            //        var propertyValue = GetPropertyValue(parentModel, propertyName);

            //        if (prototype.IsComplexType && modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
            //        {
            //            var enumerable = propertyValue as IEnumerable<dynamic>;

            //            if (enumerable != null && enumerable.Any())
            //            {
            //                if (attrList.OfType<DataTypeAttribute>().Any(a => a != null && a.DataType == DataType.Custom && a.CustomDataType == CustomDataType.Grid))
            //                {
            //                    prototype.TemplateHint = "Grid";
            //                }
            //            }
            //        }

            //        if (propertyValue == null && prototype.IsComplexType && ((modelType == typeof(ContentViewModel)) || (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(IEnumerable<>) && !(attributes.OfType<SelectionAttribute>().Any() || attributes.OfType<AdwSelectionAttribute>().Any() || attributes.OfType<AjaxSelectionAttribute>().Any()) && modelType != typeof(IEnumerable<SelectListItem>) && modelType != typeof(SelectList) && modelType != typeof(MultiSelectList))))
            //        {
            //            prototype.HideSurroundingHtml = true;
            //        }

            //        if (parentModel is AddressViewModel && propertyName == AddressViewModel.AjaxProperty)
            //        {
            //            prototype.AdditionalValues[AddressViewModel.AjaxPropertyModelMetadataKey] = true;
            //        }
            //    }
            //}

            var elementType = modelType.IsGenericType && modelType.HasElementType ? modelType.GetElementType() : modelType.GetGenericArguments().FirstOrDefault();

            // Override Template for enums
            if (modelType.GetNonNullableType().IsEnum || elementType != null && elementType.IsEnum)
            {
                prototype.TemplateHint = "Enum";
            }

            if (modelType.IsNumeric())
            {
                prototype.TemplateHint = "Number";
            }

            // Override Template for file upload properties
            if (modelType == typeof(HttpPostedFileBase))
            {
                prototype.TemplateHint = "File";
            }

            return(prototype);
        }
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype,Func<object> modelAccessor)
 {
     CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
     modelMetadata.DisplayName = prototype.DisplayName;
     return modelMetadata;
 }
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
 {
     return new CachedDataAnnotationsModelMetadata2(prototype, modelAccessor);
 }
Exemplo n.º 23
0
 public ExtendedCachedDataAnnotationsModelMetadata(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor) : base(prototype, modelAccessor)
 {
 }
        /// <summary>
        /// Creates the metadata from prototype.
        /// </summary>
        /// <param name="prototype">The prototype.</param>
        /// <param name="modelAccessor">The model accessor.</param>
        /// <returns>metadata</returns>
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            //modify the base result with your custom logic, typically adding items from prototype.AdditionalValues
            result.AdditionalValues.Add("Attributes", prototype.AdditionalValues["Attributes"]);
            var attrList   = prototype.AdditionalValues["Attributes"] as IEnumerable <Attribute>;
            var attributes = attrList;

            var propertyName  = prototype.PropertyName;
            var containerType = prototype.ContainerType;
            var modelType     = prototype.ModelType;

            if (!string.IsNullOrEmpty(prototype.PropertyName) && (containerType == null || (Nullable.GetUnderlyingType(containerType) == null)))
            {
                // Get parent model to be able to retrieve dependent property values
                var parentModel = GetParentModel(modelAccessor);

                if (parentModel != null)
                {
                    result.AdditionalValues.Add("ParentModel", parentModel);
                    result.AdditionalValues.Add("PropertyNameInParentModel", propertyName);

                    var propertyValue = GetPropertyValue(parentModel, propertyName);

                    if (prototype.IsComplexType && modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                    {
                        var enumerable = propertyValue as IEnumerable <dynamic>;

                        if (enumerable != null && enumerable.Any())
                        {
                            if (attrList.OfType <DataTypeAttribute>().Any(a => a != null && a.DataType == DataType.Custom && a.CustomDataType == CustomDataType.Grid))
                            {
                                result.TemplateHint = "Grid";
                            }
                        }
                    }

                    if (propertyValue == null && prototype.IsComplexType && ((modelType == typeof(ContentViewModel)) || (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(IEnumerable <>) && !(attributes.OfType <SelectionAttribute>().Any() || attributes.OfType <AdwSelectionAttribute>().Any() || attributes.OfType <AjaxSelectionAttribute>().Any()) && modelType != typeof(IEnumerable <SelectListItem>) && modelType != typeof(SelectList) && modelType != typeof(MultiSelectList))))
                    {
                        result.HideSurroundingHtml = true;
                    }

                    if (parentModel is AddressViewModel && propertyName == AddressViewModel.AjaxProperty)
                    {
                        result.AdditionalValues[AddressViewModel.AjaxPropertyModelMetadataKey] = true;

                        if ((parentModel as AddressViewModel).ReturnLatLongDetails)
                        {
                            result.AdditionalValues[AddressViewModel.ReturnLatLongData] = true;
                        }
                    }

                    if (parentModel is JobseekerSearchViewModel && propertyName == JobseekerSearchViewModel.AjaxProperty)
                    {
                        result.AdditionalValues[JobseekerSearchViewModel.AjaxPropertyModelMetadataKey] = true;
                    }
                }
            }


            result.TemplateHint = prototype.TemplateHint;

            return(result);
        }
        public void AttributesOverrideMetadataBooleans(
            Attribute attribute,
            Func<ModelMetadata, bool> accessor,
            bool expectedResult)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(object),
                propertyName: null,
                attributes: attributes);
            var result = accessor(metadata);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 26
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var modelMetaData = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            Composite composite;

            if (this.compositeByModelType.TryGetValue(modelMetaData.ModelType, out composite))
            {
                // CompositeType
                modelMetaData.SetComposite(composite);

                foreach (var filter in this.typeAnnotations)
                {
                    filter.OnTypeMetadataCreated(modelMetaData);
                }
            }
            else if (modelMetaData.ContainerType != null && this.compositeByModelType.TryGetValue(modelMetaData.ContainerType, out composite))
            {
                modelMetaData.SetComposite(composite);

                // Path
                if (composite != null)
                {
                    Dictionary <string, Path> pathByPropertyName;
                    if (this.pathByPropertyNameByModelType.TryGetValue(modelMetaData.ContainerType, out pathByPropertyName))
                    {
                        Path path;
                        if (pathByPropertyName.TryGetValue(modelMetaData.PropertyName, out path))
                        {
                            modelMetaData.SetPath(path);
                        }
                    }
                }

                foreach (var filter in this.propertyAnnotations)
                {
                    filter.OnPropertyMetadataCreated(modelMetaData);
                }
            }

            return(modelMetaData);
        }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <Attribute> attributes, Type containerType, Type modelType, string propertyName)
        {
            var attributesList = attributes.ToArray();

            Func <IEnumerable <Attribute>, CachedDataAnnotationsModelMetadata> metadataFactory =
                attr => base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);

            var conventionType = containerType ?? modelType;

            var defaultResourceType = DefaultResourceType;
            var conventionAttribute = conventionType.GetAttributeOnTypeOrAssembly <MetadataConventionsAttribute>();

            if (conventionAttribute != null && conventionAttribute.ResourceType != null)
            {
                defaultResourceType = conventionAttribute.ResourceType;
            }
            else if (RequireConventionAttribute)
            {
                return(metadataFactory(attributesList));
            }

            ApplyConventionsToValidationAttributes(attributesList, containerType, propertyName, defaultResourceType);

            var foundDisplayAttribute = attributesList.FirstOrDefault(a => a is DisplayAttribute) as DisplayAttribute;

            if (foundDisplayAttribute.CanSupplyDisplayName())
            {
                return(metadataFactory(attributesList));
            }

            // Our displayAttribute is lacking. Time to get busy.
            var displayAttribute = foundDisplayAttribute.Copy() ?? new DisplayAttribute();

            var rewrittenAttributes = attributesList.Replace(foundDisplayAttribute, displayAttribute);

            // ensure resource type.
            displayAttribute.ResourceType = displayAttribute.ResourceType ?? defaultResourceType;

            if (displayAttribute.ResourceType != null)
            {
                // ensure resource name
                string displayAttributeName = GetDisplayAttributeName(containerType, propertyName, displayAttribute);
                if (displayAttributeName != null)
                {
                    displayAttribute.Name = displayAttributeName;
                }
                if (!displayAttribute.ResourceType.PropertyExists(displayAttribute.Name))
                {
                    displayAttribute.ResourceType = null;
                }
            }

            CachedDataAnnotationsModelMetadata metadata = metadataFactory(rewrittenAttributes);

            if (metadata.GetDisplayName() == null || metadata.GetDisplayName() == metadata.PropertyName)
            {
                //metadata.Properties[0]..se.DisplayName = metadata.PropertyName.SplitUpperCaseToString();
            }

            return(metadata);
        }
Exemplo n.º 28
0
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
 {
     return(new CachedDataAnnotationsModelMetadata2(prototype, modelAccessor));
 }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func <object> modelAccessor)
        {
            var metadataFromPrototype = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            foreach (var keyValuePair in prototype.AdditionalValues)
            {
                metadataFromPrototype.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            // we need to preserve DisplayName fetched during prototype creation
            metadataFromPrototype.DisplayName = prototype.DisplayName;

            return(metadataFromPrototype);
        }