4

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 />
    }
Asgeir
  • 727
  • 3
  • 9
  • 20
  • Why do you want to change the name? If you change the name, MVC won't be able to bind the field back to the model when the form is submitted. You have `@name = "Sessions"` twice in your htmlAttributes parameter. It also looks like you're trying to set the id attribute to the same value on each loop, leading to duplicate ids, (unrelated to your issue, but not valid html and may break DOM lookups by id). – GWB Jun 14 '12 at 17:52
  • All of the radiobuttons get different names (the x stands for 1, 2, 3 etc), so they aren't grouped together, making me able to pick all or none of them. – Asgeir Jun 14 '12 at 17:54
  • Oh yeah, that duplicate was wrong, it's @class in my code, I did a small (incorrect) edit in the post. So bottom line, that doesn't cause any errors. – Asgeir Jun 14 '12 at 17:56
  • And the ids comes from the model sent into the view, it doesn't duplicate (it's stored in the Checkbox entity) – Asgeir Jun 14 '12 at 18:14

1 Answers1

5

If you want to be able to select only a single radio button you need to have a single property on your view model to hold the selected session id, like this:

public class SessionViewModel
{
    public int SelectedSessionId { get; set; }
    public IList<Session> Sessions { get; set; }
}

public class Session
{
    public int Id { get; set; }
    public string Name { get; set; }
}

and then have a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new SessionViewModel
        {
            SelectedSessionId = 2,
            Sessions = Enumerable.Range(1, 5).Select(x => new Session
            {
                Id = x,
                Name = "session" + x,
            }).ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SessionViewModel model)
    {
        return Content("Thank you for selecting session id: " + model.SelectedSessionId);
    }
}

and finally a view:

@model SessionViewModel

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.Sessions.Count(); i++)
    {
        @Html.HiddenFor(x => x.Sessions[i].Id)
        @Html.RadioButtonFor(x => x.SelectedSessionId, Model.Sessions[i].Id, new { id = "session_" + i })
        @Html.Label("session_" + i, Model.Sessions[i].Name)
        <br/>
    }
    <button type="submit">OK</button>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks a lot, this helped. The C# code I had was actually remarkably alike, and I acually had a SelectedSessionID which I guess I meant to use for this to begin with :P , but I don't know why but I got stuck in making the editor for the whole class (CheckBoxModel), obviously that didn't make sense. Didn't use exactly the same code, see the edit in my post. – Asgeir Jun 14 '12 at 18:45