diff --git a/src/Microsoft.AspNetCore.Mvc.TagHelpers/ValidationMessageTagHelper.cs b/src/Microsoft.AspNetCore.Mvc.TagHelpers/ValidationMessageTagHelper.cs
index 8858623013..85d8d07837 100644
--- a/src/Microsoft.AspNetCore.Mvc.TagHelpers/ValidationMessageTagHelper.cs
+++ b/src/Microsoft.AspNetCore.Mvc.TagHelpers/ValidationMessageTagHelper.cs
@@ -71,11 +71,24 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
};
}
+ string message = null;
+ if (!output.IsContentModified)
+ {
+ var tagHelperContent = await output.GetChildContentAsync();
+
+ // We check for whitespace to detect scenarios such as:
+ //
+ //
+ if (!tagHelperContent.IsEmptyOrWhiteSpace)
+ {
+ message = tagHelperContent.GetContent();
+ }
+ }
var tagBuilder = Generator.GenerateValidationMessage(
ViewContext,
For.ModelExplorer,
For.Name,
- message: null,
+ message: message,
tag: null,
htmlAttributes: htmlAttributes);
@@ -84,27 +97,12 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
output.MergeAttributes(tagBuilder);
// Do not update the content if another tag helper targeting this element has already done so.
- if (!output.IsContentModified)
+ if (!output.IsContentModified && tagBuilder.HasInnerHtml)
{
- // We check for whitespace to detect scenarios such as:
- //
- //
- var childContent = await output.GetChildContentAsync();
- if (childContent.IsEmptyOrWhiteSpace)
- {
- // Provide default message text (if any) since there was nothing useful in the Razor source.
- if (tagBuilder.HasInnerHtml)
- {
- output.Content.SetHtmlContent(tagBuilder.InnerHtml);
- }
- }
- else
- {
- output.Content.SetHtmlContent(childContent);
- }
+ output.Content.SetHtmlContent(tagBuilder.InnerHtml);
}
}
}
}
}
-}
\ No newline at end of file
+}
diff --git a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs
index 69522655cb..1c919635be 100644
--- a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs
@@ -357,10 +357,11 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
}
[Theory]
- [InlineData("Content of validation message", "Content of validation message")]
- [InlineData("\r\n \r\n", "New HTML")]
+ [InlineData("Content of validation message", "Content of validation message", "New HTML")]
+ [InlineData("\r\n \r\n", null, "New HTML")]
public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
string childContent,
+ string expectedMessage,
string expectedOutputContent)
{
// Arrange
@@ -375,7 +376,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
It.IsAny(),
It.IsAny(),
It.IsAny(),
- It.IsAny(),
+ expectedMessage,
It.IsAny(),
It.IsAny