Skip to content

Commit

Permalink
RadioButtonFor doesnt resolve isChecked properly when nested in Edito…
Browse files Browse the repository at this point in the history
…rTemplate
  • Loading branch information
brandondahler committed May 30, 2018
1 parent 8eb4d1c commit 0168394
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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

0 comments on commit 0168394

Please sign in to comment.