From 016839472e0ec1031e10214a9f3f6e236b07c6f8 Mon Sep 17 00:00:00 2001 From: Brandon Dahler Date: Wed, 30 May 2018 03:48:09 +0100 Subject: [PATCH] RadioButtonFor doesnt resolve isChecked properly when nested in EditorTemplate --- src/System.Web.Mvc/Html/InputExtensions.cs | 2 +- .../Html/Test/InputExtensionsTest.cs | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/System.Web.Mvc/Html/InputExtensions.cs b/src/System.Web.Mvc/Html/InputExtensions.cs index b52ee3157..315fb5a7a 100644 --- a/src/System.Web.Mvc/Html/InputExtensions.cs +++ b/src/System.Web.Mvc/Html/InputExtensions.cs @@ -358,7 +358,7 @@ private static MvcHtmlString RadioButtonHelper(HtmlHelper htmlHelper, ModelMetad { string valueString = Convert.ToString(value, CultureInfo.CurrentCulture); isChecked = model != null && - !String.IsNullOrEmpty(name) && + !String.IsNullOrEmpty(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)) && String.Equals(model.ToString(), valueString, StringComparison.OrdinalIgnoreCase); } diff --git a/test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs b/test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs index 5c802ef13..b99f79628 100644 --- a/test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs +++ b/test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations; using System.Data.Linq; +using System.Linq.Expressions; using System.Web.Mvc.Test; using System.Web.Routing; using System.Web.WebPages.Scope; @@ -2147,6 +2148,19 @@ public void RadioButtonForWithNameAndValue() Assert.Equal(@"", html.ToHtmlString()); } + [Fact] + public void RadioButtonForWithNestedNameAndValue() + { + // Arrange + HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonNestedAndUnsetViewData()); + + // Act + MvcHtmlString html = helper.RadioButtonFor(m => m, "ViewItemFoo"); + + // Assert + Assert.Equal(@"", html.ToHtmlString()); + } + [Fact] public void RadioButtonForWithNameAndValue_Unobtrusive() { @@ -3056,6 +3070,31 @@ private static ViewDataDictionary GetRadioButtonViewData() return viewData; } + private static ViewDataDictionary GetRadioButtonNestedAndUnsetViewData() + { + ViewDataDictionary viewData = new ViewDataDictionary { }; + viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" }; + + + Expression> containedExpression = m => m.foo; + + var metadata = ModelMetadata.FromLambdaExpression(containedExpression, viewData); + var htmlFieldName = ExpressionHelper.GetExpressionText(containedExpression); + + + ViewDataDictionary nestedViewData = new ViewDataDictionary(viewData) + { + Model = metadata.Model, + ModelMetadata = metadata, + TemplateInfo = new TemplateInfo + { + HtmlFieldPrefix = viewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName), + } + }; + + return new ViewDataDictionary(nestedViewData); + } + // TEXTBOX private static readonly RouteValueDictionary _attributesDictionary = new RouteValueDictionary(new { baz = "BazValue" }); private static readonly object _attributesObjectDictionary = new { baz = "BazObjValue" };