public DataTypeAttribute(DataType dataType)
 {
     this.DataType = dataType;
     DisplayFormatAttribute displayFormatAttribute;
     switch (dataType)
     {
         case DataType.Date:
             displayFormatAttribute = new DisplayFormatAttribute();
             displayFormatAttribute.ApplyFormatInEditMode = true;
             displayFormatAttribute.ConvertEmptyStringToNull = true;
             displayFormatAttribute.DataFormatString = "{0:d}";
             displayFormatAttribute.HtmlEncode = true;
             break;
         case DataType.Time:
             displayFormatAttribute = new DisplayFormatAttribute();
             displayFormatAttribute.ApplyFormatInEditMode = true;
             displayFormatAttribute.ConvertEmptyStringToNull = true;
             displayFormatAttribute.DataFormatString = "{0:t}";
             displayFormatAttribute.HtmlEncode = true;
             break;
         case DataType.Currency:
             displayFormatAttribute = new DisplayFormatAttribute();
             displayFormatAttribute.ApplyFormatInEditMode = false;
             displayFormatAttribute.ConvertEmptyStringToNull = true;
             displayFormatAttribute.DataFormatString = "{0:C}";
             displayFormatAttribute.HtmlEncode = true;
             break;
         default:
             displayFormatAttribute = (DisplayFormatAttribute)null;
             break;
     }
     this.DisplayFormat = displayFormatAttribute;
 }
Пример #2
0
        /// <summary>
        ///     Constructor that accepts a data type enumeration
        /// </summary>
        /// <param name="dataType">The <see cref="DataType" /> enum value indicating the type to apply.</param>
        public DataTypeAttribute(DataType dataType)
        {
            DataType = dataType;

            // Set some DisplayFormat for a few specific data types
            switch (dataType)
            {
                case DataType.Date:
                    DisplayFormat = new DisplayFormatAttribute();
                    DisplayFormat.DataFormatString = "{0:d}";
                    DisplayFormat.ApplyFormatInEditMode = true;
                    break;
                case DataType.Time:
                    DisplayFormat = new DisplayFormatAttribute();
                    DisplayFormat.DataFormatString = "{0:t}";
                    DisplayFormat.ApplyFormatInEditMode = true;
                    break;
                case DataType.Currency:
                    DisplayFormat = new DisplayFormatAttribute();
                    DisplayFormat.DataFormatString = "{0:C}";

                    // Don't set ApplyFormatInEditMode for currencies because the currency
                    // symbol can't be parsed
                    break;
            }
        }
Пример #3
0
        /// <summary>
        ///     Constructor that accepts a data type enumeration
        /// </summary>
        /// <param name="dataType">The <see cref="DataType" /> enum value indicating the type to apply.</param>
        public DataTypeAttribute(DataType dataType)
        {
            DataType = dataType;

            // Set some DisplayFormat for a few specific data types
            switch (dataType)
            {
            case DataType.Date:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString      = "{0:d}";
                DisplayFormat.ApplyFormatInEditMode = true;
                break;

            case DataType.Time:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString      = "{0:t}";
                DisplayFormat.ApplyFormatInEditMode = true;
                break;

            case DataType.Currency:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString = "{0:C}";

                // Don't set ApplyFormatInEditMode for currencies because the currency
                // symbol can't be parsed
                break;
            }
        }
Пример #4
0
        public DataTypeAttribute(DataType dataType)
        {
            DataType = dataType;

            DisplayFormatAttribute displayFormat;

            switch (dataType)
            {
            case DataType.Time:
                displayFormat = new DisplayFormatAttribute();
                displayFormat.ApplyFormatInEditMode    = true;
                displayFormat.ConvertEmptyStringToNull = true;
                displayFormat.DataFormatString         = "{0:t}";
#if NET_4_0
                displayFormat.HtmlEncode = true;
#endif
                break;

            case DataType.Date:
                displayFormat = new DisplayFormatAttribute();
                displayFormat.ApplyFormatInEditMode    = true;
                displayFormat.ConvertEmptyStringToNull = true;
                displayFormat.DataFormatString         = "{0:d}";
#if NET_4_0
                displayFormat.HtmlEncode = true;
#endif
                break;

            case DataType.Currency:
                displayFormat = new DisplayFormatAttribute();
                displayFormat.ApplyFormatInEditMode    = false;
                displayFormat.ConvertEmptyStringToNull = true;
                displayFormat.DataFormatString         = "{0:C}";
#if NET_4_0
                displayFormat.HtmlEncode = true;
#endif
                break;

            default:
                displayFormat = null;
                break;
            }

            DisplayFormat = displayFormat;
        }
		public DataTypeAttribute (DataType dataType)
		{
			DataType = dataType;

			DisplayFormatAttribute displayFormat;
			switch (dataType) {
				case DataType.Time:
					displayFormat = new DisplayFormatAttribute ();
					displayFormat.ApplyFormatInEditMode = true;
					displayFormat.ConvertEmptyStringToNull = true;
					displayFormat.DataFormatString = "{0:t}";
					break;

				default:
					displayFormat = null;
					break;
			}

			DisplayFormat = displayFormat;
		}
Пример #6
0
		public DataTypeAttribute (DataType dataType)
		{
			DataType = dataType;

			DisplayFormatAttribute displayFormat;
			switch (dataType) {
				case DataType.Time:
					displayFormat = new DisplayFormatAttribute ();
					displayFormat.ApplyFormatInEditMode = true;
					displayFormat.ConvertEmptyStringToNull = true;
					displayFormat.DataFormatString = "{0:t}";
#if NET_4_0
					displayFormat.HtmlEncode = true;
#endif
					break;
				case DataType.Date:
					displayFormat = new DisplayFormatAttribute ();
					displayFormat.ApplyFormatInEditMode = true;
					displayFormat.ConvertEmptyStringToNull = true;
					displayFormat.DataFormatString = "{0:d}";
#if NET_4_0
					displayFormat.HtmlEncode = true;
#endif
					break;
				case DataType.Currency:
					displayFormat = new DisplayFormatAttribute ();
					displayFormat.ApplyFormatInEditMode = false;
					displayFormat.ConvertEmptyStringToNull = true;
					displayFormat.DataFormatString = "{0:C}";
#if NET_4_0
					displayFormat.HtmlEncode = true;
#endif
					break;

				default:
					displayFormat = null;
					break;
			}

			DisplayFormat = displayFormat;
		}
Пример #7
0
        public DataTypeAttribute(DataType dataType)
        {
            DataType = dataType;

            DisplayFormatAttribute displayFormat;

            switch (dataType)
            {
            case DataType.Time:
                displayFormat = new DisplayFormatAttribute();
                displayFormat.ApplyFormatInEditMode    = true;
                displayFormat.ConvertEmptyStringToNull = true;
                displayFormat.DataFormatString         = "{0:t}";
                break;

            default:
                displayFormat = null;
                break;
            }

            DisplayFormat = displayFormat;
        }
Пример #8
0
        public DataTypeAttribute(DataType dataType)
        {
            this.DataType = dataType;
            DisplayFormatAttribute displayFormatAttribute;

            switch (dataType)
            {
            case DataType.Date:
                displayFormatAttribute = new DisplayFormatAttribute();
                displayFormatAttribute.ApplyFormatInEditMode    = true;
                displayFormatAttribute.ConvertEmptyStringToNull = true;
                displayFormatAttribute.DataFormatString         = "{0:d}";
                displayFormatAttribute.HtmlEncode = true;
                break;

            case DataType.Time:
                displayFormatAttribute = new DisplayFormatAttribute();
                displayFormatAttribute.ApplyFormatInEditMode    = true;
                displayFormatAttribute.ConvertEmptyStringToNull = true;
                displayFormatAttribute.DataFormatString         = "{0:t}";
                displayFormatAttribute.HtmlEncode = true;
                break;

            case DataType.Currency:
                displayFormatAttribute = new DisplayFormatAttribute();
                displayFormatAttribute.ApplyFormatInEditMode    = false;
                displayFormatAttribute.ConvertEmptyStringToNull = true;
                displayFormatAttribute.DataFormatString         = "{0:C}";
                displayFormatAttribute.HtmlEncode = true;
                break;

            default:
                displayFormatAttribute = (DisplayFormatAttribute)null;
                break;
            }
            this.DisplayFormat = displayFormatAttribute;
        }
        public void EditFormatString_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "custom format";
            var dataType = new DataTypeAttribute(DataType.Currency);
            var displayFormat = new DisplayFormatAttribute
            {
                ApplyFormatInEditMode = true,
                DataFormatString = expected,
            };

            var provider = CreateProvider(new object[] { displayFormat, dataType, });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.EditFormatString;

            // Assert
            Assert.Equal(expected, result);
        }
        public void DataTypeName_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "MultilineText";
            var dataType = new DataTypeAttribute(DataType.MultilineText);
            var displayFormat = new DisplayFormatAttribute { HtmlEncode = false, };
            var provider = CreateProvider(new object[] { dataType, displayFormat });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.DataTypeName;

            // Assert
            Assert.Equal(expected, result);
        }
        public void DataTypeName_Html_IfHtmlEncodeFalse()
        {
            // Arrange
            var expected = "Html";
            var displayFormat = new DisplayFormatAttribute { HtmlEncode = false, };
            var provider = CreateProvider(new[] { displayFormat });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.DataTypeName;

            // Assert
            Assert.Equal(expected, result);
        }
        public void DataTypeName_Null_IfHtmlEncodeTrue()
        {
            // Arrange
            var displayFormat = new DisplayFormatAttribute { HtmlEncode = true, };
            var provider = CreateProvider(new[] { displayFormat });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.DataTypeName;

            // Assert
            Assert.Null(result);
        }
        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);
        }
        private static void SetFromDataTypeAndDisplayAttributes(DataAnnotationsModelMetadata result,
            DataTypeAttribute dataTypeAttribute, DisplayFormatAttribute displayFormatAttribute)
        {
            if (dataTypeAttribute != null)
            {
                result.DataTypeName = dataTypeAttribute.ToDataTypeName();
            }

            if (displayFormatAttribute == null && dataTypeAttribute != null)
            {
                displayFormatAttribute = dataTypeAttribute.DisplayFormat;

                // If DisplayFormat value was non-null and this [DataType] is of a subclass, assume the [DataType]
                // constructor used the protected DisplayFormat setter to override its default. Note deriving from
                // [DataType] but preserving DataFormatString and ApplyFormatInEditMode results in
                // HasNonDefaultEditFormat==true.
                if (displayFormatAttribute != null && dataTypeAttribute.GetType() != typeof(DataTypeAttribute))
                {
                    result.HasNonDefaultEditFormat = true;
                }
            }
            else if (displayFormatAttribute != null)
            {
                result.HasNonDefaultEditFormat = true;
            }

            if (displayFormatAttribute != null)
            {
                result.NullDisplayText = displayFormatAttribute.NullDisplayText;
                result.DisplayFormatString = displayFormatAttribute.DataFormatString;
                result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
                result.HtmlEncode = displayFormatAttribute.HtmlEncode;

                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    result.EditFormatString = displayFormatAttribute.DataFormatString;
                }

                if (!displayFormatAttribute.HtmlEncode && String.IsNullOrWhiteSpace(result.DataTypeName))
                {
                    result.DataTypeName = DataTypeUtil.HtmlTypeName;
                }

                // Regardless of HasNonDefaultEditFormat calculation above, treat missing EditFormatString as the
                // default.  Note the corner case of a [DataType] subclass overriding a non-empty default to apply a
                // [DisplayFormat] lacking DataFormatString or with ApplyFormatInEditMode==false results in
                // HasNonDefaultEditFormat==false.
                if (String.IsNullOrEmpty(result.EditFormatString))
                {
                    result.HasNonDefaultEditFormat = false;
                }
            }
        }
 public DerivedDataTypeAttribute()
     : base(DataType.Time)
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         ApplyFormatInEditMode = true,
         DataFormatString = DerivedDataTypeAttributeFormatString,
     };
 }
 public DataTypeWithCustomDisplayFormat() : base("Custom datatype")
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         DataFormatString = "value",
     };
 }
 public DataTypeWithCustomEditFormat() : base("Custom datatype")
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         ApplyFormatInEditMode = true,
         DataFormatString = "value",
     };
 }
Пример #18
0
		DisplayFormatAttribute GetDisplayFormat ()
		{
			MetaModel.GetDataFieldAttribute <DisplayFormatAttribute> (Attributes, ref displayFormatAttr);
			if (displayFormatAttr == null) {
				var dta = DataTypeAttribute;
				displayFormatAttr = dta == null ? null : dta.DisplayFormat;
			}
			
			return displayFormatAttr;
		}