Skip to content
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

RadioButtonFor doesn't resolve isChecked properly when nested in EditorTemplate #164

Closed
brandondahler opened this issue May 24, 2018 · 4 comments
Assignees
Labels
3 - Done bug cost: S Will take up to 2 days to complete PRI: 2 - Preferred Preferably should be handled during the milestone. up-for-grabs
Milestone

Comments

@brandondahler
Copy link
Contributor

Problem

When using RadioButtonFor and specifying a default expression of m => m, the checked attribute isn't set as expected.

While default expressions of m => m aren't generally used (or even valid at times, unless the call is nested under another model), they are distinctly valid and necessary in EditorTemplates when overriding a default template.

Example

Models/IndexModel.cs

namespace Test
{
    public class IndexModel
    {
        public bool? Boolean { get; set; }
    }
}

Controllers/HomeController.cs

namespace Test
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new IndexModel() { Boolean = true });
        }
    }
}

Views/Home/Index.cshtml

@model Test.Models.IndexModel


@Html.EditorFor(m => m.Boolean)

Views/Home/EditorTemplates/Boolean.cshtml

@Html.RadioButtonFor(m => m, true)
@Html.LabelFor(m => m, "True")

Expected Output

<input checked="checked" id="Boolean" name="Boolean" type="radio" value="True">
<label for="Boolean">True</label>

Actual Output

<input id="Boolean" name="Boolean" type="radio" value="True">
<label for="Boolean">True</label>

Possible Solution

I believe this is caused by the following line:

!String.IsNullOrEmpty(name) &&

I believe that this issue would be resolved if we replaced that line with the following:

        !String.IsNullOrEmpty(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)) &&
@dougbu
Copy link
Member

dougbu commented May 29, 2018

@brandondahler this sounds like a reasonable fix for your scenario. We would happily accept a PR that makes the change you describe.

@dougbu dougbu added up-for-grabs bug 1 - Ready cost: S Will take up to 2 days to complete PRI: 2 - Preferred Preferably should be handled during the milestone. labels May 29, 2018
@dougbu dougbu added this to the 3.2.7 milestone May 29, 2018
@dougbu
Copy link
Member

dougbu commented Jun 23, 2018

0168394

@dougbu dougbu closed this as completed Jun 23, 2018
@dougbu dougbu self-assigned this Jun 23, 2018
@dougbu dougbu added 3 - Done and removed 1 - Ready labels Jun 23, 2018
@dougbu
Copy link
Member

dougbu commented Jun 23, 2018

8b1a545 fixes a very similar issue

@dougbu
Copy link
Member

dougbu commented Jun 23, 2018

@brandondahler thanks again for your help here ❕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 - Done bug cost: S Will take up to 2 days to complete PRI: 2 - Preferred Preferably should be handled during the milestone. up-for-grabs
Projects
None yet
Development

No branches or pull requests

2 participants