Pass IEnumerable list to controller

后端 未结 1 1117
耶瑟儿~
耶瑟儿~ 2020-12-21 12:03

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.

相关标签:
1条回答
  • 2020-12-21 13:01

    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.

    0 讨论(0)
提交回复
热议问题