ASP.NET MVC RadioButtonFor Not Selected On Load

979 Views Asked by At

There are many other questions here on Stack Overflow and elsewhere, but I have spent far too much time sifting through them for answers to not finally ask one myself. I have tried many of the solutions found here and none of them work.

My problem is as stated in the title; my radio buttons are not being set on page load and I can't figure out as to why.

In the code below my model has a boolean value CanSubmit, the canSubmitYes and canSubmitNo id's are generated id's using

ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("canSubmitYes")

and

ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("canSubmitNo")

My radio buttons and corresponding labels are currently set with the following:

@Html.LabelFor(Function(m) m.CanSubmit, "Yes", New With {.for = canSubmitYes})
@Html.RadioButtonFor(Function(m) m.CanSubmit, True, New With {.id = canSubmitYes, .class = "submit-toggle-yes"})

@Html.LabelFor(Function(m) m.CanSubmit, "No", New With {.for = canSubmitNo})
@Html.RadioButtonFor(Function(m) m.CanSubmit, False, New With {.id = canSubmitNo, .class = "submit-toggle-no"})

What I have tried:

  • Setting the checked="checked" properties in the the helpers HTMLAttributes collection
  • Strip out all of the HTMLHelper code stuff and simply use basic <input type="radio" /> controls and for some reason it's STILL not being checked properly on load.
  • Tried using a jQuery event to set the 'checked' property of them on load of the document, this too, did not work. (via .prop("checked", true))
  • Changed my format of the html helper from "false" to false to try to force it to understand that the property is a boolean and not a string

There are no other javascript events setting values to these controls. When I observe me clicking the buttons there are no HTML values changed on the objects - which leads me to think that the 'clicked'/'checked' property is a pseudo property or state that the browser holds.

Have I missed something critical or am I way off base here?

Thanks in advance!

Update

I have determined that it has something to do with the local variables I'm using. I now have them in basic <input>'s again and if I don't connect them they work as expected. It is only when I try to pair them with a common name (the way to tie them together) that they don't work.

<input name="radio@(Model.ID)" id="@canSubmitYes" type="radio" class="submit-toggle-yes" @(If(submitterPermission.IsSelected, "checked='checked'", "")) />
<label for="@canSubmitYes">Yes</label>

<input name="radio@(Model.ID)" id="@canSubmitNo" type="radio" class="submit-toggle-no" @(If(submitterPermission.IsSelected, "", "checked='checked'")) />
<label for="@canSubmitNo">No</label>

If I remove the name='' attribute then it correctly checks the proper element on load. I need these items to be unique as there could potentially be 50+ of such parings on the page.

1

There are 1 best solutions below

0
alexpwalsh On BEST ANSWER

I just found that I had left this question unanswered.

We eventually did not find the problem, but ended up following a different approach. Furthermore I no longer work on that project so I cannot adequately update this question.