Skip to content

Commit 9c6ba84

Browse files
authored
Merge pull request #165 from brandondahler/bugfix/NestedRadioButtonFor
RadioButtonFor doesnt resolve isChecked properly when nested
2 parents 052c9c2 + 0168394 commit 9c6ba84

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/System.Web.Mvc/Html/InputExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private static MvcHtmlString RadioButtonHelper(HtmlHelper htmlHelper, ModelMetad
358358
{
359359
string valueString = Convert.ToString(value, CultureInfo.CurrentCulture);
360360
isChecked = model != null &&
361-
!String.IsNullOrEmpty(name) &&
361+
!String.IsNullOrEmpty(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)) &&
362362
String.Equals(model.ToString(), valueString, StringComparison.OrdinalIgnoreCase);
363363
}
364364

test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.ComponentModel.DataAnnotations;
55
using System.Data.Linq;
6+
using System.Linq.Expressions;
67
using System.Web.Mvc.Test;
78
using System.Web.Routing;
89
using System.Web.WebPages.Scope;
@@ -2147,6 +2148,19 @@ public void RadioButtonForWithNameAndValue()
21472148
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""ViewDataFoo"" />", html.ToHtmlString());
21482149
}
21492150

2151+
[Fact]
2152+
public void RadioButtonForWithNestedNameAndValue()
2153+
{
2154+
// Arrange
2155+
HtmlHelper<string> helper = MvcHelper.GetHtmlHelper(GetRadioButtonNestedAndUnsetViewData());
2156+
2157+
// Act
2158+
MvcHtmlString html = helper.RadioButtonFor(m => m, "ViewItemFoo");
2159+
2160+
// Assert
2161+
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""ViewItemFoo"" />", html.ToHtmlString());
2162+
}
2163+
21502164
[Fact]
21512165
public void RadioButtonForWithNameAndValue_Unobtrusive()
21522166
{
@@ -3056,6 +3070,31 @@ private static ViewDataDictionary<FooBarModel> GetRadioButtonViewData()
30563070
return viewData;
30573071
}
30583072

3073+
private static ViewDataDictionary<string> GetRadioButtonNestedAndUnsetViewData()
3074+
{
3075+
ViewDataDictionary<FooBarModel> viewData = new ViewDataDictionary<FooBarModel> { };
3076+
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" };
3077+
3078+
3079+
Expression<Func<FooBarModel, string>> containedExpression = m => m.foo;
3080+
3081+
var metadata = ModelMetadata.FromLambdaExpression(containedExpression, viewData);
3082+
var htmlFieldName = ExpressionHelper.GetExpressionText(containedExpression);
3083+
3084+
3085+
ViewDataDictionary nestedViewData = new ViewDataDictionary(viewData)
3086+
{
3087+
Model = metadata.Model,
3088+
ModelMetadata = metadata,
3089+
TemplateInfo = new TemplateInfo
3090+
{
3091+
HtmlFieldPrefix = viewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName),
3092+
}
3093+
};
3094+
3095+
return new ViewDataDictionary<string>(nestedViewData);
3096+
}
3097+
30593098
// TEXTBOX
30603099
private static readonly RouteValueDictionary _attributesDictionary = new RouteValueDictionary(new { baz = "BazValue" });
30613100
private static readonly object _attributesObjectDictionary = new { baz = "BazObjValue" };

0 commit comments

Comments
 (0)