ASP.NETMVCに単純なフォームがあります。これらの結果をコントローラーアクションに投稿しようとしていますが、奇妙な動作をしています。
はview単純なHTMLテーブルです。
HTMLフォームビューの一部は次のとおりです。
<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>
バインディングオブジェクト:
public class SurveyUpdater
{
public Question[] questions { get; set; }
}
public class Question
{
public int[] responseIds { get; set; }
}
コントローラアクションコード:
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");
}
テスト後、これが私の観察です:
各質問で少なくとも1つを
CheckBox選択している場合、これは正常に機能し、コントローラーupdater.questions.Length == 5でデータが完全にバインドされます。質問の1つにまったく答えないと、スキップした数と同じ大きさの配列しか得られません
-1。したがって、質問3に答えなかった場合、コントローラーアクション2で配列を取得します。#2のロジックを使用することで、最初の質問に答えない場合は、次のようになります
null。updater.questions
私が得たいもの(そして私が期待したもの)はそれです:
私は常にquestions長さを取得し、5質問の1つに答えなかった場合は、0そのインデックスのサイズの配列を取得するだけでしたresponseIds。
これはASP.NETMVCモデルバインディングのバグですか?そうでない場合、私が見逃しているもの、または私が探している望ましい動作を取得する方法はありますか?
