private static void AddAttribute(DisplayMetadataProviderContext context, string value, string keyname) { if (!string.IsNullOrEmpty(value)) { context.DisplayMetadata.AdditionalValues[keyname] = value; } }
/// <inheritdoc /> public virtual void GetDisplayMetadata(DisplayMetadataProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } foreach (var provider in _providers.OfType<IDisplayMetadataProvider>()) { provider.GetDisplayMetadata(context); } }
public void GetDisplayMetadata(DisplayMetadataProviderContext context) { if (context.Key.ModelType == typeof(LargeModelWithValidation)) { context.DisplayMetadata.AdditionalValues.Add("key1", _guid); context.DisplayMetadata.AdditionalValues.Add("key2", "value2"); } var displayAttribute = context.Attributes.OfType<DisplayAttribute>().FirstOrDefault(); var groupName = displayAttribute?.GroupName; if (!string.IsNullOrEmpty(groupName)) { context.DisplayMetadata.AdditionalValues[GroupNameKey] = groupName; } }
public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType( Type type, IEnumerable <KeyValuePair <string, string> > expectedKeyValuePairs) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert Assert.Equal(expectedKeyValuePairs, context.DisplayMetadata.EnumDisplayNamesAndValues); }
public void GetDisplayMetadata_EnumNamesAndValues_ReflectsModelType( Type type, IReadOnlyDictionary <string, string> expectedDictionary) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert // This assertion does *not* require entry orders to match. Assert.Equal(expectedDictionary, context.DisplayMetadata.EnumNamesAndValues); }
public void GetDisplayMetadata_SimpleAttributes( object attribute, Func<DisplayMetadata, object> accessor, object expected) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, new ModelAttributes(new object[] { attribute })); // Act provider.GetDisplayMetadata(context); // Assert var value = accessor(context.DisplayMetadata); Assert.Equal(expected, value); }
public void GetDisplayMetadata_FindsDisplayFormat_FromDataType() { // Arrange var provider = new DataAnnotationsMetadataProvider(); var dataType = new DataTypeAttribute(DataType.Currency); var displayFormat = dataType.DisplayFormat; // Non-null for DataType.Currency. var attributes = new[] { dataType, }; var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString); }
public void GetDisplayMetadata_SimpleAttributes( object attribute, Func <DisplayMetadata, object> accessor, object expected) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, new ModelAttributes(new object[] { attribute })); // Act provider.GetDisplayMetadata(context); // Assert var value = accessor(context.DisplayMetadata); Assert.Equal(expected, value); }
public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType( Type type, IEnumerable <KeyValuePair <string, string> > expectedKeyValuePairs) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert // OrderBy is used because the order of the results may very depending on the platform / client. Assert.Equal( expectedKeyValuePairs?.OrderBy(item => item.Key, StringComparer.Ordinal), context.DisplayMetadata.EnumDisplayNamesAndValues?.OrderBy(item => item.Key, StringComparer.Ordinal)); }
/// <summary> /// Gets the values for properties of <see cref="T:Microsoft.AspNet.Mvc.ModelBinding.Metadata.DisplayMetadata"/>. /// </summary> /// <param name="context">The <see cref="T:Microsoft.AspNet.Mvc.ModelBinding.Metadata.DisplayMetadataProviderContext"/>.</param> public void GetDisplayMetadata(DisplayMetadataProviderContext context) { var propertyAttributes = context.Attributes; var modelMetadata = context.DisplayMetadata; var propertyName = context.Key.Name; var typeName = context.Key.ModelType.FullName; var name = context.Key.Name; if (IsTransformRequired(propertyName, modelMetadata, propertyAttributes)) { if (name.Length > 2 && (typeName.Contains("Int") || (typeName.Contains("Guid")))) { var match = IdRegex.Match(name); if (match.Success) { name = match.Groups[1].Value; } } Func<string> a = () => name.Humanize().Transform(To.TitleCase); modelMetadata.DisplayName = a; } }
public void GetDisplayMetadata_FindsDisplayFormat_OverridingDataType() { // Arrange var provider = new DataAnnotationsMetadataProvider(); var dataType = new DataTypeAttribute(DataType.Time); // Has a non-null DisplayFormat. var displayFormat = new DisplayFormatAttribute() // But these values override the values from DataType { DataFormatString = "Cool {0}", }; var attributes = new Attribute[] { dataType, displayFormat, }; var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString); }
public void GetDisplayMetadata_DisplayAttribute_DescriptionFromResources() { // Arrange var provider = new DataAnnotationsMetadataProvider(); var display = new DisplayAttribute() { #if USE_REAL_RESOURCES Description = nameof(Test.Resources.DisplayAttribute_Description), ResourceType = typeof(Test.Resources), #else Description = nameof(DataAnnotations.Test.Resources.DisplayAttribute_Description), ResourceType = typeof(TestResources), #endif }; var attributes = new Attribute[] { display }; var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert Assert.Equal("description from resources", context.DisplayMetadata.Description()); }
/// <inheritdoc /> public void GetDisplayMetadata(DisplayMetadataProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var attributes = context.Attributes; var dataTypeAttribute = attributes.OfType<DataTypeAttribute>().FirstOrDefault(); var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault(); var displayColumnAttribute = attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(); var displayFormatAttribute = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault(); var hiddenInputAttribute = attributes.OfType<HiddenInputAttribute>().FirstOrDefault(); var scaffoldColumnAttribute = attributes.OfType<ScaffoldColumnAttribute>().FirstOrDefault(); var uiHintAttribute = attributes.OfType<UIHintAttribute>().FirstOrDefault(); // Special case the [DisplayFormat] attribute hanging off an applied [DataType] attribute. This property is // non-null for DataType.Currency, DataType.Date, DataType.Time, and potentially custom [DataType] // subclasses. The DataType.Currency, DataType.Date, and DataType.Time [DisplayFormat] attributes have a // non-null DataFormatString and the DataType.Date and DataType.Time [DisplayFormat] attributes have // ApplyFormatInEditMode==true. if (displayFormatAttribute == null && dataTypeAttribute != null) { displayFormatAttribute = dataTypeAttribute.DisplayFormat; } var displayMetadata = context.DisplayMetadata; // ConvertEmptyStringToNull if (displayFormatAttribute != null) { displayMetadata.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull; } // DataTypeName if (dataTypeAttribute != null) { displayMetadata.DataTypeName = dataTypeAttribute.GetDataTypeName(); } else if (displayFormatAttribute != null && !displayFormatAttribute.HtmlEncode) { displayMetadata.DataTypeName = DataType.Html.ToString(); } // Description if (displayAttribute != null) { displayMetadata.Description = () => displayAttribute.GetDescription(); } // DisplayFormatString if (displayFormatAttribute != null) { displayMetadata.DisplayFormatString = displayFormatAttribute.DataFormatString; } // DisplayName if (displayAttribute != null) { displayMetadata.DisplayName = () => displayAttribute.GetName(); } // EditFormatString if (displayFormatAttribute != null && displayFormatAttribute.ApplyFormatInEditMode) { displayMetadata.EditFormatString = displayFormatAttribute.DataFormatString; } // IsEnum et cetera var underlyingType = Nullable.GetUnderlyingType(context.Key.ModelType) ?? context.Key.ModelType; var underlyingTypeInfo = underlyingType.GetTypeInfo(); if (underlyingTypeInfo.IsEnum) { // IsEnum displayMetadata.IsEnum = true; // IsFlagsEnum displayMetadata.IsFlagsEnum = underlyingTypeInfo.GetCustomAttribute<FlagsAttribute>(inherit: false) != null; // EnumDisplayNamesAndValues and EnumNamesAndValues // // Order EnumDisplayNamesAndValues to match Enum.GetNames(). That method orders by absolute value, // then its behavior is undefined (but hopefully stable). Add to EnumNamesAndValues in same order but // Dictionary does not guarantee order will be preserved. var groupedDisplayNamesAndValues = new List<KeyValuePair<EnumGroupAndName, string>>(); var namesAndValues = new Dictionary<string, string>(); foreach (var name in Enum.GetNames(underlyingType)) { var field = underlyingType.GetField(name); var displayName = GetDisplayName(field); var groupName = GetDisplayGroup(field); var value = ((Enum)field.GetValue(obj: null)).ToString("d"); groupedDisplayNamesAndValues.Add(new KeyValuePair<EnumGroupAndName, string>( new EnumGroupAndName(groupName, displayName), value)); namesAndValues.Add(name, value); } displayMetadata.EnumGroupedDisplayNamesAndValues = groupedDisplayNamesAndValues; displayMetadata.EnumNamesAndValues = namesAndValues; } // HasNonDefaultEditFormat if (!string.IsNullOrEmpty(displayFormatAttribute?.DataFormatString) && displayFormatAttribute?.ApplyFormatInEditMode == true) { // Have a non-empty EditFormatString based on [DisplayFormat] from our cache. if (dataTypeAttribute == null) { // Attributes include no [DataType]; [DisplayFormat] was applied directly. displayMetadata.HasNonDefaultEditFormat = true; } else if (dataTypeAttribute.DisplayFormat != displayFormatAttribute) { // Attributes include separate [DataType] and [DisplayFormat]; [DisplayFormat] provided override. displayMetadata.HasNonDefaultEditFormat = true; } else if (dataTypeAttribute.GetType() != typeof(DataTypeAttribute)) { // Attributes include [DisplayFormat] copied from [DataType] and [DataType] was of a subclass. // Assume the [DataType] constructor used the protected DisplayFormat setter to override its // default. That is derived [DataType] provided override. displayMetadata.HasNonDefaultEditFormat = true; } } // HideSurroundingHtml if (hiddenInputAttribute != null) { displayMetadata.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue; } // HtmlEncode if (displayFormatAttribute != null) { displayMetadata.HtmlEncode = displayFormatAttribute.HtmlEncode; } // NullDisplayText if (displayFormatAttribute != null) { displayMetadata.NullDisplayText = displayFormatAttribute.NullDisplayText; } // Order if (displayAttribute?.GetOrder() != null) { displayMetadata.Order = displayAttribute.GetOrder().Value; } // ShowForDisplay if (scaffoldColumnAttribute != null) { displayMetadata.ShowForDisplay = scaffoldColumnAttribute.Scaffold; } // ShowForEdit if (scaffoldColumnAttribute != null) { displayMetadata.ShowForEdit = scaffoldColumnAttribute.Scaffold; } // SimpleDisplayProperty if (displayColumnAttribute != null) { displayMetadata.SimpleDisplayProperty = displayColumnAttribute.DisplayColumn; } // TemplateHint if (uiHintAttribute != null) { displayMetadata.TemplateHint = uiHintAttribute.UIHint; } else if (hiddenInputAttribute != null) { displayMetadata.TemplateHint = "HiddenInput"; } }
/// <inheritdoc /> public void GetDisplayMetadata([NotNull] DisplayMetadataProviderContext context) { var attributes = context.Attributes; var dataTypeAttribute = attributes.OfType <DataTypeAttribute>().FirstOrDefault(); var displayAttribute = attributes.OfType <DisplayAttribute>().FirstOrDefault(); var displayColumnAttribute = attributes.OfType <DisplayColumnAttribute>().FirstOrDefault(); var displayFormatAttribute = attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); var hiddenInputAttribute = attributes.OfType <HiddenInputAttribute>().FirstOrDefault(); var scaffoldColumnAttribute = attributes.OfType <ScaffoldColumnAttribute>().FirstOrDefault(); var uiHintAttribute = attributes.OfType <UIHintAttribute>().FirstOrDefault(); // Special case the [DisplayFormat] attribute hanging off an applied [DataType] attribute. This property is // non-null for DataType.Currency, DataType.Date, DataType.Time, and potentially custom [DataType] // subclasses. The DataType.Currency, DataType.Date, and DataType.Time [DisplayFormat] attributes have a // non-null DataFormatString and the DataType.Date and DataType.Time [DisplayFormat] attributes have // ApplyFormatInEditMode==true. if (displayFormatAttribute == null && dataTypeAttribute != null) { displayFormatAttribute = dataTypeAttribute.DisplayFormat; } var displayMetadata = context.DisplayMetadata; // ConvertEmptyStringToNull if (displayFormatAttribute != null) { displayMetadata.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull; } // DataTypeName if (dataTypeAttribute != null) { displayMetadata.DataTypeName = dataTypeAttribute.GetDataTypeName(); } else if (displayFormatAttribute != null && !displayFormatAttribute.HtmlEncode) { displayMetadata.DataTypeName = DataType.Html.ToString(); } // Description if (displayAttribute != null) { displayMetadata.Description = () => displayAttribute.GetDescription(); } // DisplayFormat if (displayFormatAttribute != null) { displayMetadata.DisplayFormatString = displayFormatAttribute.DataFormatString; } // DisplayName if (displayAttribute != null) { displayMetadata.DisplayName = () => displayAttribute.GetName(); } if (displayFormatAttribute != null && displayFormatAttribute.ApplyFormatInEditMode) { displayMetadata.EditFormatString = displayFormatAttribute.DataFormatString; } // IsEnum et cetera var underlyingType = Nullable.GetUnderlyingType(context.Key.ModelType) ?? context.Key.ModelType; var underlyingTypeInfo = underlyingType.GetTypeInfo(); if (underlyingTypeInfo.IsEnum) { // IsEnum displayMetadata.IsEnum = true; // IsFlagsEnum displayMetadata.IsFlagsEnum = underlyingTypeInfo.GetCustomAttribute <FlagsAttribute>(inherit: false) != null; // EnumDisplayNamesAndValues and EnumNamesAndValues // // Order EnumDisplayNamesAndValues to match Enum.GetNames(). That method orders by absolute value, // then its behavior is undefined (but hopefully stable). Add to EnumNamesAndValues in same order but // Dictionary does not guarantee order will be preserved. var displayNamesAndValues = new List <KeyValuePair <string, string> >(); var namesAndValues = new Dictionary <string, string>(); foreach (var name in Enum.GetNames(underlyingType)) { var field = underlyingType.GetField(name); var displayName = GetDisplayName(field); var value = ((Enum)field.GetValue(obj: null)).ToString("d"); displayNamesAndValues.Add(new KeyValuePair <string, string>(displayName, value)); namesAndValues.Add(name, value); } displayMetadata.EnumDisplayNamesAndValues = displayNamesAndValues; displayMetadata.EnumNamesAndValues = namesAndValues; } // HasNonDefaultEditFormat if (!string.IsNullOrEmpty(displayFormatAttribute?.DataFormatString) && displayFormatAttribute?.ApplyFormatInEditMode == true) { // Have a non-empty EditFormatString based on [DisplayFormat] from our cache. if (dataTypeAttribute == null) { // Attributes include no [DataType]; [DisplayFormat] was applied directly. displayMetadata.HasNonDefaultEditFormat = true; } else if (dataTypeAttribute.DisplayFormat != displayFormatAttribute) { // Attributes include separate [DataType] and [DisplayFormat]; [DisplayFormat] provided override. displayMetadata.HasNonDefaultEditFormat = true; } else if (dataTypeAttribute.GetType() != typeof(DataTypeAttribute)) { // Attributes include [DisplayFormat] copied from [DataType] and [DataType] was of a subclass. // Assume the [DataType] constructor used the protected DisplayFormat setter to override its // default. That is derived [DataType] provided override. displayMetadata.HasNonDefaultEditFormat = true; } } // HideSurroundingHtml if (hiddenInputAttribute != null) { displayMetadata.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue; } // HtmlEncode if (displayFormatAttribute != null) { displayMetadata.HtmlEncode = displayFormatAttribute.HtmlEncode; } // NullDisplayText if (displayFormatAttribute != null) { displayMetadata.NullDisplayText = displayFormatAttribute.NullDisplayText; } // Order if (displayAttribute?.GetOrder() != null) { displayMetadata.Order = displayAttribute.GetOrder().Value; } // ShowForDisplay if (scaffoldColumnAttribute != null) { displayMetadata.ShowForDisplay = scaffoldColumnAttribute.Scaffold; } // ShowForEdit if (scaffoldColumnAttribute != null) { displayMetadata.ShowForEdit = scaffoldColumnAttribute.Scaffold; } // SimpleDisplayProperty if (displayColumnAttribute != null) { displayMetadata.SimpleDisplayProperty = displayColumnAttribute.DisplayColumn; } // TemplateHint if (uiHintAttribute != null) { displayMetadata.TemplateHint = uiHintAttribute.UIHint; } else if (hiddenInputAttribute != null) { displayMetadata.TemplateHint = "HiddenInput"; } }
public void GetDisplayDetails_DisplayAttribute_NameFromResources() { // Arrange var provider = new DataAnnotationsMetadataProvider(); var display = new DisplayAttribute() { #if USE_REAL_RESOURCES Name = nameof(Test.Resources.DisplayAttribute_Name), ResourceType = typeof(Test.Resources), #else Name = nameof(Test.Resources.DisplayAttribute_Name), ResourceType = typeof(Test.TestResources), #endif }; var attributes = new Attribute[] { display }; var key = ModelMetadataIdentity.ForType(typeof(string)); var context = new DisplayMetadataProviderContext(key, attributes); // Act provider.GetDisplayMetadata(context); // Assert Assert.Equal("name from resources", context.DisplayMetadata.DisplayName); }
public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType( Type type, IEnumerable<KeyValuePair<string, string>> expectedKeyValuePairs) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert // OrderBy is used because the order of the results may very depending on the platform / client. Assert.Equal( expectedKeyValuePairs?.OrderBy(item => item.Key, StringComparer.Ordinal), context.DisplayMetadata.EnumDisplayNamesAndValues?.OrderBy(item => item.Key, StringComparer.Ordinal)); }
public void GetDisplayDetails_EnumDisplayNamesAndValues_ReflectsModelType( Type type, IEnumerable<KeyValuePair<string, string>> expectedKeyValuePairs) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, attributes); // Act provider.GetDisplayMetadata(context); // Assert Assert.Equal(expectedKeyValuePairs, context.DisplayMetadata.EnumDisplayNamesAndValues); }
public void GetDisplayMetadata_EnumNamesAndValues_ReflectsModelType( Type type, IReadOnlyDictionary<string, string> expectedDictionary) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert // This assertion does *not* require entry orders to match. Assert.Equal(expectedDictionary, context.DisplayMetadata.EnumNamesAndValues); }
public void GetDisplayMetadata_IsFlagsEnum_ReflectsModelType(Type type, bool expectedIsFlagsEnum) { // Arrange var provider = new DataAnnotationsMetadataProvider(); var key = ModelMetadataIdentity.ForType(type); var attributes = new object[0]; var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes)); // Act provider.GetDisplayMetadata(context); // Assert Assert.Equal(expectedIsFlagsEnum, context.DisplayMetadata.IsFlagsEnum); }
public void GetDisplayMetadata(DisplayMetadataProviderContext context) { var displayAttribute = context.Attributes.OfType<DisplayAttribute>().FirstOrDefault(); AddAttribute(context, displayAttribute?.Prompt, nameof(displayAttribute.Prompt)); AddAttribute(context, displayAttribute?.ShortName, nameof(displayAttribute.ShortName)); }