I'm trying to group together radiobuttons that are creating using a for loop and Razor syntax. Here is the code:
@for (var i = 0; i < Model.Sessions.Count(); i++)
{
@Html.HiddenFor(it => it.Sessions[i].Id)
@Html.RadioButtonFor(it => it.Sessions[i].Checkbox, "0", new {@class = "Sessions", @id = id, @name="Sessions"})
@Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
<span class="time-span"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
<br />
}
The third line inside the for loop is where the problem is. Basically the name doesn't change and it's always "Sessions[x].Checkbox". The checkbox is a property (bool) of a custom class. I can't seem to get the hang of debugging Razor stuff, so any help would be greatly appreciated, I'm guessing this will be extremely obvious to someone here.
EDIT Dimitrov's post helped a lot. Below is the final code I used. I use the @class and @id attributes to be able to use Javascript to select the session originally picked (since this is an edit, not create form).
@for (var i = 0; i < Model.Sessions.Count(); i++)
{
@Html.HiddenFor(it => it.Sessions[i].Id)
var SId = @Model.Sessions[i].Id;
@Html.RadioButtonFor(it => it.selectedSession, Model.Sessions[i].Id, new { id = SId, @class = "Sessions" })
@Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
<span class="time-span"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
<br />
}