Skip to content

RadioButtonFor doesnt resolve isChecked properly when nested #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Web.Mvc/Html/InputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
39 changes: 39 additions & 0 deletions test/System.Web.Mvc.Test/Html/Test/InputExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2147,6 +2148,19 @@ public void RadioButtonForWithNameAndValue()
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""ViewDataFoo"" />", html.ToHtmlString());
}

[Fact]
public void RadioButtonForWithNestedNameAndValue()
{
// Arrange
HtmlHelper<string> helper = MvcHelper.GetHtmlHelper(GetRadioButtonNestedAndUnsetViewData());

// Act
MvcHtmlString html = helper.RadioButtonFor(m => m, "ViewItemFoo");

// Assert
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""ViewItemFoo"" />", html.ToHtmlString());
}

[Fact]
public void RadioButtonForWithNameAndValue_Unobtrusive()
{
Expand Down Expand Up @@ -3056,6 +3070,31 @@ private static ViewDataDictionary<FooBarModel> GetRadioButtonViewData()
return viewData;
}

private static ViewDataDictionary<string> GetRadioButtonNestedAndUnsetViewData()
{
ViewDataDictionary<FooBarModel> viewData = new ViewDataDictionary<FooBarModel> { };
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" };


Expression<Func<FooBarModel, string>> 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<string>(nestedViewData);
}

// TEXTBOX
private static readonly RouteValueDictionary _attributesDictionary = new RouteValueDictionary(new { baz = "BazValue" });
private static readonly object _attributesObjectDictionary = new { baz = "BazObjValue" };
Expand Down