From 89ebce02897f808cf166a8f008ff71fb4e4490dc Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Mon, 27 Apr 2015 14:42:05 -0700 Subject: [PATCH] [Fixes #2336] Generate some input elements without value attribute --- .../Rendering/Html/DefaultHtmlGenerator.cs | 18 ++++++++++++++++- .../InputTagHelper.cs | 11 +++++----- ...elpersWebSite.MvcTagHelper_Home.Input.html | 20 +++++++++---------- .../InputTagHelperTest.cs | 20 ++++++++++++------- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultHtmlGenerator.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultHtmlGenerator.cs index 7cf3b012a5..5e53d76a4a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultHtmlGenerator.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultHtmlGenerator.cs @@ -1024,7 +1024,23 @@ namespace Microsoft.AspNet.Mvc.Rendering attributeValue = useViewData ? EvalString(viewContext, fullName, format) : valueParameter; } - tagBuilder.MergeAttribute("value", attributeValue, replaceExisting: isExplicitValue); + var addValue = true; + object typeAttributeValue; + if (htmlAttributes != null && htmlAttributes.TryGetValue("type", out typeAttributeValue)) + { + if (string.Equals(typeAttributeValue.ToString(), "file", StringComparison.OrdinalIgnoreCase) || + string.Equals(typeAttributeValue.ToString(), "image", StringComparison.OrdinalIgnoreCase)) + { + // 'value' attribute is not needed for 'file' and 'image' input types. + addValue = false; + } + } + + if (addValue) + { + tagBuilder.MergeAttribute("value", attributeValue, replaceExisting: isExplicitValue); + } + break; } diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs index 12e5aee175..c9538add68 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs @@ -281,13 +281,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers format = GetFormat(modelExplorer, inputTypeHint, inputType); } - object htmlAttributes = null; + var htmlAttributes = new Dictionary + { + { "type", inputType } + }; + if (string.Equals(inputType, "file") && string.Equals(inputTypeHint, TemplateRenderer.IEnumerableOfIFormFileName)) { - htmlAttributes = new Dictionary - { - { "multiple", "multiple" } - }; + htmlAttributes["multiple"] = "multiple"; } return Generator.GenerateTextBox( diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Input.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Input.html index 0cdec96faa..ff75fad2c5 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Input.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Input.html @@ -7,16 +7,16 @@

Input Tag Helper Test

- - - - - + + + + + - - - - - + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs index ced7f834ac..fee26dadc0 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs @@ -623,6 +623,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers [InlineData("TEXT", null, "not-null")] [InlineData("custom-datatype", null, null)] [InlineData(null, "unknown-input-type", "not-null")] + [InlineData("Image", null, "not-null")] + [InlineData(null, "image", "not-null")] public async Task ProcessAsync_CallsGenerateTextBox_WithExpectedParameters( string dataTypeName, string inputTypeName, @@ -695,7 +697,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelper.For.Name, model, // value null, // format - null)) // htmlAttributes + It.Is>(m => m.ContainsKey("type")))) // htmlAttributes .Returns(tagBuilder) .Verifiable(); @@ -791,13 +793,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var tagBuilder = new TagBuilder("input", new NullTestEncoder()); - Dictionary htmlAttributes = null; + var htmlAttributes = new Dictionary + { + { "type", expectedType } + }; if (string.Equals(dataTypeName, TemplateRenderer.IEnumerableOfIFormFileName)) { - htmlAttributes = new Dictionary - { - { "multiple", "multiple" } - }; + htmlAttributes["multiple"] = "multiple"; } htmlGenerator .Setup(mock => mock.GenerateTextBox( @@ -860,6 +862,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers SelfClosing = true, }; + var htmlAttributes = new Dictionary + { + { "type", expectedType } + }; var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var htmlGenerator = new Mock(MockBehavior.Strict); var tagHelper = GetTagHelper( @@ -877,7 +883,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelper.For.Name, null, // value expectedFormat, - null)) // htmlAttributes + htmlAttributes)) // htmlAttributes .Returns(tagBuilder) .Verifiable();