I have this in a view:
@{
var i = 0;
}
@foreach (var field in Model.ConsentQuestions)
{
<div class="form-group">
<div class="control-label col-md-8">
@Html.Label(string.Format("ConsentQuestions[{0}].Key", i), field.Key)
@Html.Hidden(string.Format("ConsentQuestions[{0}].Key", i), field.Key)
</div>
<p class="col-md-2">
@Html.RadioButton(string.Format("ConsentQuestions[{0}].Value", i), true, htmlAttributes: new { id = string.Format("question{0}-true", i) })
<label for="question@(i)-true">Yes</label>
@Html.RadioButton(string.Format("ConsentQuestions[{0}].Value", i), false, htmlAttributes: new { id = string.Format("question{0}-false", i) })
<label for="question@(i)-false">No</label>
</p>
</div>
i++;
}
Model.ConsentQuestions
is an IEnumerable<KeyValuePair<string, bool>>
(the reason for this is that the questions are user-definable). For whatever reason, the binder can't figure out this one out. Usually this sort of indexing works fine with collections (I am doing something similar with other IEnumerables with no issue). What's strange is if I breakpoint my validating method, it sees that there are the right number of items in ConsentQuestions, except each KVP is {"", false}.
I'd like to know how to correct the problem and get the values in the form to bind.
edit: I do have a seemingly working solution in using a class that inherits from DefaultModelBinder
and then overrides GetPropertyValue
to look right at controllerContext.HttpContext.Request.Form
... while that's fine as far as it goes I'd still like to know why in this case it isn't working.