In my asp.net MVC 4 project, I\'m trying to pass IEnumerable list from view to controller. The problem is that the list receiverd in the action is null. Any help please.
The problem is in fact that this expression:
@Html.HiddenFor(modelItem => item.ID_agent)
and similar as well cannot derive a correct name for the HTML input control, and the resulting request parameters are not parsed by model binder. Usually this is fixed by replacing foreach
with for
:
@fore (int i=0; i<Model.Count; i++)
{
@Html.HiddenFor(modelItem => modelItem[i].ID_agent)
@Html.RadioButtonFor(modelItem => modelItem[i].SelectedAgent, modelItem[i].ID_agent)
@Html.DisplayFor(modelItem => modelItem[i].nom_agent)
@Html.DisplayFor(modelItem => modelItem[i].prenom_agent)
<br />
}
Note that you would need your view to be typed with IList<>
or an array to allow this behavior.