Reputation: 13916
I have tried to group a set of radio buttons inside loop by providing additional html attribute in html help as below -
<ol class="Opt">
@foreach (var opt in quest.Options)
{
<li class="Opt">
@Html.RadioButtonFor(o => opt.Title, opt.Title, new { @name = "uniqueRadio"})
@Html.Label(opt.Title)
</li>
}
</ol>
However name attribute ot generated input html tag gets over-written by opt.Title for obvious reasons. MVC-4 uses name attribute for strongly typed model-binding when posting data.
How do I make radio button grouped together ?
EDIT: I replaced RadioButtonFor with RadioButton, as suggested below. But this way I miss-out model binding feature.
<ol class="Question">
@for (int j = 0; j < Model.Options.Count; j++)
{
<li class="Opt">
@Html.RadioButton("uniqueRadio", Model.Options[j].IsSelected, false)
@Model.Options[j].Title
</li>
}
</ol>
Upvotes: 4
Views: 20896
Reputation: 1471
Use simple RadioButton
<ol class="Opt">
@foreach (var opt in quest.Options)
{
<li class="Opt">
@Html.RadioButton("uniqueRadio", opt.Title)
@Html.Label(opt.Title)
</li>
}
</ol
Upvotes: 4