I have a simple form in ASP.NET MVC. I am trying to post these results to a controller action and I am getting strange behavior.
The view
is a simple HTML table:
Here is part of the HTML form View:
<form action="/Applications/UpdateSurvey" method="post"><table id=questionsTable class=questionsTable border=1>
<thead><tr><td>Name</td><td>Answer</td><td>Name Attribute(for debugging)</td></tr> </thead><tbody>
<tr>
<td>Question 0:</td>
<td><input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=1 >1 <input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=2 >2 <input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=3 >3 <input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=4 >4 <input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=5 >5 </td>
<td>updater.questions[0].responseIds</td>
</tr>
<tr>
<td>Question 1:</td>
<td><input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=1 >1 <input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=2 >2 <input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=3 >3 <input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=4 >4 <input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=5 >5 </td>
<td>updater.questions[1].responseIds</td>
</tr>
</tbody></table>
<input type="submit" value="Save" />
</form>
Binding Object:
public class SurveyUpdater
{
public Question[] questions { get; set; }
}
public class Question
{
public int[] responseIds { get; set; }
}
Controller Action code:
public ActionResult UpdateSurvey(SurveyUpdater updater)
{
if (updater.questions == null)
{
//I dont understand why this is getting hit
}
if (updater.questions.Length != 5)
{
//I dont understand why this is getting hit
}
return View("TestSurvey");
}
After testing, here are my observations:
If I have at least one
CheckBox
selected on each of the questions, this works fine and in my controllerupdater.questions.Length == 5
and the data binds perfectly.If I don't answer one of the questions at all, I only get an array as big as the number I skipped:
-1
. So if I didn't answer Question #3, I get an array in my controller action of 2.By using the logic of #2, if I don't answer the first question, I simply get
null
forupdater.questions
What I want to get (and what I expected) is that:
I would always get questions
with a length of 5
and in the cases where I didn't answer one of the questions, I would simply get a 0
sized array for that index responseIds
.
Is this a bug in ASP.NET MVC model binding? If not, is there anything I am missing or any way to get the desired behavior that I am looking for?