changus
changus

Reputation: 763

ASP.NET MVC 3 getting values from dynamic radiobuttons

I have a VERY similar question to this question:

ASP.NET MVC C# dynamic radiobuttons getting formcollection

However, the only difference is, instead of naming the names as:

name="radios[0]" and name="radios[1]", with an 0 and 1, respectively, I need to start mine at 1.

When it comes into public ActionResult Update(string[] radios), it is null because it does not start at 0. I am trying to get this to work using a Dictionary<int, int> radios, and having my names as:

name="radios[1][1 through 3]" and then name="radios[2][1 through 3]" but it doesn't seem to be working. Any ideas? I can not just subtract 1 from all the values because that's not necessarily the case, I said I am using 1 as an example. Thanks, any help would be great!!

Upvotes: 0

Views: 1120

Answers (1)

Martin Booth
Martin Booth

Reputation: 8595

Take a look at phil haacked blog entry, specifically the part: Non-Sequential Indices

To summerise, you need to add 3 new hidden fields, all called radios.Index, one with the value 1 , one with value 2 and the other with the value of 3

e.g.

<input type="hidden" name="radios.Index" value="1" />
<input type="hidden" name="radios.Index" value="2" />
<input type="hidden" name="radios.Index" value="3" />

Martin

Upvotes: 2

Related Questions