views:

931

answers:

1

Hello, I am rather new to MVC.

I am trying to post data (An array of type "SurveyAnswer") to a MVC action method from an HTML form.

Everything works fine with I pass back data from a html input that has a single value, such as a Html.TextBox(), but when the html input has multiple values such as Html.ListBox(), the action method in my controller only takes the first of those values and moves on to the next uniquely named input in the array. Here is an example:

Here is what my Html form looks like after it is rendered:

<form action="/test/VoteConfirmation" method="post">

<li>Test RadioButton Question<br/>
<input id="answersArray[0]_SurveyQuestionID" name="answersArray[0].SurveyQuestionID" type="hidden" value="2" />
<ul>
<li>Jane Doe 1<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="13" /></li>
<li>Jane Doe 2<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="14" /></li>
<li>Jane Doe 3<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="15" /></li>
<li>Jane Doe 4<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="16" /></li>
<li>Jane Doe 5<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="17" /></li>
<li>Jane Doe 6<input id="answersArray[0]_SurveyOptionID" name="answersArray[0].SurveyOptionID" type="radio" value="18" /></li>
</ul>
</li>

<li>Test SingleList Question<br/>
<input id="answersArray[1]_SurveyQuestionID" name="answersArray[1].SurveyQuestionID" type="hidden" value="3" />
<select id="answersArray[1]_SurveyOptionID" name="answersArray[1].SurveyOptionID">
<option value="19">Jane Doe 7</option>
<option value="20">Jane Doe 8</option>
<option value="21">Jane Doe 9</option>
</select>
</li>

<li>Test TextBox Question<br/>
<input id="answersArray[2]_SurveyQuestionID" name="answersArray[2].SurveyQuestionID" type="hidden" value="4" />
<input id="answersArray[2]_AnswerText" name="answersArray[2].AnswerText" type="text" value="" /></li>

<li>Test DateChoice Question<br/>
<input id="answersArray[3]_SurveyQuestionID" name="answersArray[3].SurveyQuestionID" type="hidden" value="5" />
<input id="answersArray[3]_AnswerText" name="answersArray[3].AnswerText" type="text" value="" />
</li>

<li>Test NumChoice Question<br/>
<input id="answersArray[4]_SurveyQuestionID" name="answersArray[4].SurveyQuestionID" type="hidden" value="6" />
<input id="answersArray[4]_AnswerText" name="answersArray[4].AnswerText" type="text" value="" />
</li>


<li>Test MultiList Question<br/>
<input id="answersArray[5]_SurveyQuestionID" name="answersArray[5].SurveyQuestionID" type="hidden" value="7" />
<select id="answers[5]_SurveyOptionID" multiple="multiple" name="answers[5].SurveyOptionID">
<option value="22">Jane Doe 10</option>
<option value="23">Jane Doe 11</option>
<option value="24">Jane Doe 12</option>
</select></li>

<input type="submit" />

</form>

As you can see, when the user makes selections in the first 5 inputs, they are posted to the action method. The Hidden inputs hold information that will not change, in this case, the SurveyQuestionID, and the other inputs, such as TextBoxes hold user modifiable data such as SurveyAnswer.AnswerText.

The problem comes when the the ListBox data is parsed by the actionMethod. It looks at the first selected value available for answers[5].SurveyOptionID uses that, ignoring any other selected values and moving on to the next input (if there was on, there is not in this example).

How do I get these multiple selections!? I have tried everything I can think of :(. Please help,

Thanks in advance, Matt

+1  A: 

Can you show us the action method's signature? Is it taking an array argument?

public ActionResult ProcessQuestionnaire(Object[] answersArray, Object[] answers)
jeef3