1

I've been through many questions about this same issue and even browsed the RadiobuttonFor helpers source code but I just can't get my head around this seemingly very simple issue.

The problem is, the generated input-element (type radio) from RadioButtonFor-helper will not have the checked-attribute even though the value is in the model. I'm using Asp.Net MVC 5.2.3.

I have a simple example of the problem here: Model:

public class MyModel
{
    public string Name { get; set; }
    [UIHint("Gender")]
    public string Gender { get; set; }

    public MyModel(string name, string gender)
    {
        Name = name;
        Gender = gender;
    }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Models.MyModel defaultModel = new Models.MyModel("Jack", "M");

        return View(defaultModel);
    }
}

The thing is, everything works fine if I don't use an EditorTemplate ie:

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

<label>
    @Html.RadioButtonFor(m => m.Gender, "M")
    Male
</label>

<label>
    @Html.RadioButtonFor(m => m.Gender, "F")
    Female
</label>

Result:

enter image description here

But when I use an EditorTemplate for the Gender the checked-attribute will not be there:

"Parent view"

@Html.EditorFor(m => m.Name)
@Html.EditorFor(m => m.Gender)

EditorTemplate (in ControllerName/EditorTemplates/Gender.cshtml-file)

@model String

<label>
    @Html.RadioButtonFor(m => m, "M")
    Male
</label>

<label>
    @Html.RadioButtonFor(m => m, "F")
    Female
</label>

Result:

enter image description here

<input class="text-box single-line" id="Name" name="Name" type="text" value="Jack">
<label>
    <input id="Gender" name="Gender" type="radio" value="M">
    Male
</label>

<label>
    <input id="Gender" name="Gender" type="radio" value="F">
    Female
</label>

Why is this and how should it be handled? I tried to browse the source code for RadioButtonFor and it should set the "isChecked" to true if model value and the radiobuttons value-parameter match. Even tried to debug the MVC source code but my Visual Studio was unable to download symbols for some reason :)

Any help appreciated.

Esko
  • 4,109
  • 2
  • 22
  • 37
  • I think your issue similar to this post: https://stackoverflow.com/questions/8357468/radiobuttonfor-not-selecting-value-in-editor-templates. You can create a custom HTML helper as alternative for `EditorTemplates`. – Tetsuya Yamamoto Nov 23 '17 at 09:07
  • What is the actual html that is being generated for the radio buttons when you use the `EditorTemplate`? –  Nov 23 '17 at 09:10
  • In my opinion the answer in that linked question does not answer my question, why does the RadioButtonFor act differently when in EditorTemplate? I know I can just use RadioButton, but that doesn't explain why I have to do it so silly. – Esko Nov 23 '17 at 09:41
  • @StephenMuecke Added html. – Esko Nov 23 '17 at 09:43
  • @Esko, I have the source code and will debug it shortly (and as a side note, you should add `new { id = "" }` to remove the `id` attribute so you do not generate invalid html) –  Nov 23 '17 at 09:47
  • @StephenMuecke Thank you very much! I'm aware of the id-problem it's not in my production code :) – Esko Nov 23 '17 at 09:48
  • @Esko, I can confirm that the dupe is correct (being the way to solve it in an EditorTemplate when the model is a simple property), although I think the description of the issue is not quite complete. Unfortunately I only have the source code for MVC-3 on this device and I'll check it for MVC-5 tomorrow. –  Nov 23 '17 at 10:46
  • based on the SourceCode link, you have to set manually the checked value as the logic i read in the link code – Hope Mystery Nov 23 '17 at 10:56
  • @StephenMuecke Thank you for your time :) – Esko Nov 23 '17 at 12:45
  • @Esko, Rather than re-opening this and adding an answer to explain why this is happening, I have asked Darin if I can edit his answer. In the meantime, your can insect the [source code here](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/InputExtensions.cs). The `RadiobuttonFor()` methods call the `private static MvcHtmlString RadioButtonHelper()` and pass the `name` argument using `ExpressionHelper.GetExpressionText(expression)`. –  Nov 23 '17 at 22:59
  • When `expression` is `m => m.Gender` it returns `"Gender"` (the name of the property), but when `expression` is `m => m` it returns an empty string which is why `IsChecked = model != null && !String.IsNullOrEmpty(name) && String.Equals(model.ToString(), valueString, StringComparison.OrdinalIgnoreCase);` returns `false` and therefore the checked attribute is not set in the subsequent code –  Nov 23 '17 at 23:18
  • 1
    I consider this is a bug since it breaks model binding (and the work around in the dupe should not be necessary) and will probably post an Issue on the GitHub site later –  Nov 23 '17 at 23:18
  • 1
    FYI - I have now logged an [Issue on GitHub](https://github.com/aspnet/Mvc/issues/7080) –  Nov 24 '17 at 05:01
  • 1
    @Esko. The issue has now been moved [to here](https://github.com/aspnet/AspNetWebStack/issues/108). Its been fixed in asp.net-core-mvc but I think its unlikely that any change will be made to MVC-5 because it would break other code –  Nov 28 '17 at 06:23
  • @StephenMuecke Thank you very much for your valuable input on this, greatly appreciated. I had an inkling that there was something wrong in the MVC framework. What you recon I should do about this question? – Esko Nov 28 '17 at 07:10
  • @Esko, Not sure if I should re-open this with my privileges and add an answer to explain the issue (Darin has not responded to my request to edit his answer) –  Nov 28 '17 at 07:12
  • @StephenMuecke Maybe I should post a meta question about this and ask, I will consider that :) – Esko Nov 28 '17 at 07:14
  • @Esko, I was considering doing the same thing. I could just go ahead and edit the dupe, but I don't feel comfortable doing that considering I asked and had no response. And it feels like an abuse to re-open this and add an answer just to give a few extra lines of explanation and then repeat Darins 'workaround'. I'll leave it to you to post on meta –  Nov 28 '17 at 07:19

0 Answers0