Correct minor problem in `CachedDataAnnotationsModelMetadata.ComputeDisplayFormatString()`

- only affects an extreme corner case: user sets `metadata.EditFormatString` then reads
  `metadata.DisplayFormatString`
- an extreme case because `EditFormatString` is normally set only when
  `DisplayFormatString` is set and, if set, it's to the same value
- happened to see this while updating `CachedDataAnnotationsModelMetadata` for this PR

nit: an -> a in an adjacent XML comment in `CachedDataAnnotationsModelMetadata`
This commit is contained in:
Doug Bunting 2014-10-01 13:05:16 -07:00
parent a5600a74a3
commit 0d92a829ff
2 changed files with 43 additions and 5 deletions

View File

@ -81,19 +81,19 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Calculate <see cref="ModelMetadata.DisplayFormatString"/> based on presence of an
/// Calculate <see cref="ModelMetadata.DisplayFormatString"/> based on presence of a
/// <see cref="DisplayFormatAttribute"/> and its <see cref="DisplayFormatAttribute.DataFormatString"/> value.
/// </summary>
/// <returns>
/// Calculated <see cref="ModelMetadata.DisplayFormatString"/> value.
/// <see cref="DisplayFormatAttribute.DataFormatString"/> if an <see cref="DisplayFormatAttribute"/> exists.
/// <see cref="DisplayFormatAttribute.DataFormatString"/> if a <see cref="DisplayFormatAttribute"/> exists.
/// <c>null</c> otherwise.
/// </returns>
protected override string ComputeDisplayFormatString()
{
return PrototypeCache.DisplayFormat != null
? PrototypeCache.DisplayFormat.DataFormatString
: base.ComputeEditFormatString();
: base.ComputeDisplayFormatString();
}
protected override string ComputeDisplayName()
@ -114,13 +114,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Calculate <see cref="ModelMetadata.EditFormatString"/> based on presence of an
/// Calculate <see cref="ModelMetadata.EditFormatString"/> based on presence of a
/// <see cref="DisplayFormatAttribute"/> and its <see cref="DisplayFormatAttribute.ApplyFormatInEditMode"/> and
/// <see cref="DisplayFormatAttribute.DataFormatString"/> values.
/// </summary>
/// <returns>
/// Calculated <see cref="ModelMetadata.DisplayFormatString"/> value.
/// <see cref="DisplayFormatAttribute.DataFormatString"/> if an <see cref="DisplayFormatAttribute"/> exists and
/// <see cref="DisplayFormatAttribute.DataFormatString"/> if a <see cref="DisplayFormatAttribute"/> exists and
/// its <see cref="DisplayFormatAttribute.ApplyFormatInEditMode"/> is <c>true</c>; <c>null</c> otherwise.
/// </returns>
/// <remarks>

View File

@ -321,6 +321,44 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal(expected, result);
}
[Fact]
private void EditFormatString_DoesNotAffectDisplayFormat()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
var metadata = new CachedDataAnnotationsModelMetadata(
provider,
containerType: null,
modelType: typeof(object),
propertyName: null,
attributes: Enumerable.Empty<Attribute>());
// Act
metadata.EditFormatString = "custom format";
// Assert
Assert.Null(metadata.DisplayFormatString);
}
[Fact]
private void DisplayFormatString_DoesNotAffectEditFormat()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
var metadata = new CachedDataAnnotationsModelMetadata(
provider,
containerType: null,
modelType: typeof(object),
propertyName: null,
attributes: Enumerable.Empty<Attribute>());
// Act
metadata.DisplayFormatString = "custom format";
// Assert
Assert.Null(metadata.EditFormatString);
}
private class DataTypeWithCustomDisplayFormat : DataTypeAttribute
{
public DataTypeWithCustomDisplayFormat() : base("Custom datatype")