views:

119

answers:

1

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:

Picture of view

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&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=2 >2&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=3 >3&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=4 >4&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=5 >5&nbsp;&nbsp;</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&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=2 >2&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=3 >3&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=4 >4&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=5 >5&nbsp;&nbsp;</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:

  1. If I have at least one CheckBox selected on each of the questions, this works fine and in my controller updater.questions.Length == 5 and the data binds perfectly.

  2. 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.

  3. By using the logic of #2, if I don't answer the first question, I simply get null for updater.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?

+4  A: 

The problem, I think, is because when no selections are chosen, the inputs are not even passed back in the request parameters. One way around this would be to have a default, hidden checkbox containing a known value that you could filter out selected initially for each question (a "not answered" checkbox, if you will). That would guarantee that you get a selection for each question and that a request parameter exists for each element in the array.

Think about it from the perspective of what gets posted back. Only those elements that have values, have names, and aren't disabled get posted. If not all the questions have values, then how many array items should it create? At best it can guess that the last item selected should be the size of the array -- but what values should it use for any items in between? The framework can't read your mind and, arguably, shouldn't though providing the default value for the type might be reasonable. IMO, it would be better to just omit the value and, thus, force the developer to provide the default if desired. That seems to be what is happening.

tvanfosson
good idea.. i put this in front of every row . .<div id="divCheckbox" style="visibility: hidden;"><input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=0 checked >
ooo
and i now discard element 0 in the controller action check . ..
ooo