Mateusz Rogulski
Mateusz Rogulski

Reputation: 7445

How to handle multiple radiobuttons in asp.net mvc3?

I have table like:

id value

3 var1
6 var2
81 var3

so I need display two radiobutton's for each record like:

var1
O Yes
O No

var2
O Yes
O No

var3
O Yes
O No

Where "O" is radiobutton.

But I have no idea how I can handle it in controller. There can be more records of course. How I should named them or something else. Any help would be appreciated.

Upvotes: 1

Views: 405

Answers (2)

ssilas777
ssilas777

Reputation: 9804

Have you tried some approach like this?

//Model

public class MyModel
{
   public int SomeProperty { get; set; }
   public int SomeOtherProperty { get; set; } 

   public IList<MyDetails> RadioButtonList{ get; set; }
}

public class MyDetails
{
   public string Name { get; set; }
   public string Id { get; set; }
}

// Controller

public ActionResult Index()
{
  MyModel myModel = new MyModel()
  {
   RadioButtonList = getListFromDB();
   SomeProperty  = valuse       
  };

  return View(myModel);
}

//View

@foreach (var item in Model.RadioButtonList)
{
   <b>@item.Name</b>
   @Html.RadioButton("@item.Id", "0", true); <span> Yes </span><br />
   @Html.RadioButton("@item.Id", "1", false); <span> No </span><br />   
}

Upvotes: 1

Franky
Franky

Reputation: 651

In order for this to work, you need to give each pair of radio button a distinct group name. This ensures, that only yes or no for each combination can be selected.

Upvotes: 0

Related Questions