Reputation: 610
I'm trying to bind values of radio buttons to List<int>
. I can see the value is preperly submitted in Fiddler but in Visual Studio the value of List<int> data
is always null in MyAction
View
@using (Html.BeginForm("MyAction", "MyModel")) {
@foreach (SomeClass s in Model) {
for(x=1 ; x< 5; x++){
@Html.RadioButton("data[" + s.ID + "]", x)}
}
<input type="submit" value="submit" />
}
}
HTML
<input id="data_1_" name="data[1]" type="radio" value="1">
<input id="data_1_" name="data[1]" type="radio" value="2">
<input id="data_1_" name="data[1]" type="radio" value="3">
<input id="data_1_" name="data[1]" type="radio" value="4">
<input id="data_1_" name="data[1]" type="radio" value="5">
<input id="data_2_" name="data[2]" type="radio" value="1">
<input id="data_2_" name="data[2]" type="radio" value="2">
<input id="data_2_" name="data[2]" type="radio" value="3">
<input id="data_2_" name="data[2]" type="radio" value="4">
<input id="data_2_" name="data[2]" type="radio" value="5">
Action
[HttpPost]
public ActionResult MyAction(List<int> data) {
return View();
}
Upvotes: 1
Views: 963
Reputation: 12618
The index must be zero-based and unbroken.
Here is blog about posting collections of objects, but i am sure that same rule apply to collections of primitives
From the same blog, for collection of primitives you don't need index at all. Just use same name for all of them
That's was answer on you question, but according to your code you have different problem:
As long as you using radio buttons, you don't need collection at all. Give them same name, and in action accept primitive:
HTML:
<input id="data_1_" name="data" type="radio" value="1">
<input id="data_2_" name="data" type="radio" value="2">
Action:
[HttpPost]
public ActionResult MyAction(int data) {
return View();
}
Upvotes: 3