View to Controller in mvc3

僤鯓⒐⒋嵵緔 提交于 2019-12-02 03:11:27

You need to index the Html.*For items as such;

@Html.RadioButtonFor(m => m[i].SelectedOption, item.Option3, item)

To make things simplier, i'd probably get rid of the foreach & and separate i declaration and use the following;

@for(int i=0; i < Model.Count; i++)
{
    @Html.HiddenFor(m => m[i].QuestionID) 
    @Html.RadioButtonFor(m => m[i].SelectedOption, Model[i].Option3, Model[i])
}

etc.

Indexing like this will cause the html to be rendered with the indexing intact:

<input type='hidden' name=[0].'QuestionId' />
<input type='hidden' name=[1].'QuestionId' />
<input type='hidden' name=[2].'QuestionId' />

Rather than what you're doing currently, which ends up rendering as so;

<input type='hidden' name='QuestionId' />
<input type='hidden' name='QuestionId' />
<input type='hidden' name='QuestionId' />

Without the indexing, each form field is given the same name, so you're controller is going to think only one was returned.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!