0

I tried to follow this but the default modelbinder let my array null on the server side.

HTML:

Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list[1]" type="radio" value="1002" />No
...

Controller action:

 public ActionResult Anamnesis(string[] list)
 {

If I choose only the second "No" (list[0] is missing) then the DefaultModelBinder is impossible to transform it into an array.

Thanx in advance!

Update#1

Reformatted based on the comment, thank you!

Update#2

Just a tought: created a hidden input after all list item, and in this way it works. But it's ugly, no doubt.

Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes   
<input type="hidden" name="list[0]"/>
Question 2:
<input name="list[1]" type="radio" value="1002" />No
<input type="hidden" name="list[1]"/>
...

Order it's very important: the hidden value submits only when the radio is unchecked. The idea it's from the ASP.NET MVC helpers. (Btw I cannot use Html.RadioButton to archive this behavior.)

4
  • 1
    Arrays are zero based in C#. Would be a start. Commented Jun 17, 2009 at 22:56
  • True! Works great if I choose the first element - but if I select other option (list[2]) it will fail. It seems the default binder needs a zero-based array. But that's not possible with radio buttons...may I should switch to jQuery to create requests. Commented Jun 17, 2009 at 23:15
  • If these are to be radio buttons, i.e., they are mutually exclusive (choose one or the other), then you should give them the same name. Otherwise, if the names are different, they are treated as separate "sets", and it would be possible to have them //both// selected! (And no way to un-select.) -- Mike Commented Jun 17, 2009 at 23:36
  • Thanks, I'll correct my example. Commented Jun 17, 2009 at 23:44

2 Answers 2

0

Your Update #2 seems like it solves your problem. Your Update #2 is also interesting in that you could also use this approach to supply a default value (such as 999) to be used whenever nothing is checked.

There is perhaps another similar way to do what you are asking, which is based on this article and which also uses hidden inputs. The idea is that you can create indexes for each of your radio sets, to avoid the situation where a missing selection earlier in the form causes all subsequent selections to be dropped:

Question 1:
<input name="list.Index" type="hidden" value="0" />
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list.Index" type="hidden" value="1" />
<input name="list[1]" type="radio" value="1000" />No
<input name="list[1]" type="radio" value="1001" />Yes

The reason I suggest this, is in the case where you might like to associate your answers with a specific question by a unique ID, instead of just using 0, 1, 2 etc. The article I linked will show an example of how to do this.

Good luck!
-Mike

Sign up to request clarification or add additional context in comments.

1 Comment

Cleaner solution then mine, thanx!
-1

The name attribute of the radio button should be list, not list[n].

1 Comment

This is a list of possible answers for question. First question's answers are list[0], second question's answers list[1] etc. If I write just "list" instead of indexing it, the user can select ony one answer per page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.