c# – Asp.Net MVC表单,具有未知的Controller参数

我正在构建一个问卷mvc webapp,我无法弄清楚如何从表单中将未知数量的参数传递给控制器​​.

我的表格是这样的:

<% using (Html.BeginForm())
   { %>
<div id="Content">
    <% foreach (var group in ViewData.Model.QuestionGroups)
       { %>
    <div class="Group">
        <%=group.Description %>
        <% foreach (var question in group.Questions)
           {%>
        <div class="Question">
            <div class="QuestionTitle">
                <%=question.Title %>
            </div>
                <%=Html.Hidden("Id",question.ID) %>
            <div class="QuestionText">
                <%switch (question.TypeAsEnum)
                  {
                      case QuestionTypeEnum.Text:%>
                <%=Html.TextBox("somename") %>
                <% break;
                      case QuestionTypeEnum.Number:%>
                <%=Html.TextBox("somename") %>
                <% break;
                      case QuestionTypeEnum.PhoneNumber:%>
                <%=Html.TextBox("somename")%>
                <% break;
                      case QuestionTypeEnum.Email:%>
                <%=Html.TextBox("somename")%>
                <% break;
                      case QuestionTypeEnum.Date:%>
                <%=Html.TextBox("somename")%>
                <% break;
                      case QuestionTypeEnum.YesNo:%>
                <%=Html.RadioButton("somename",true)%>
                <%=Html.RadioButton("somename",false)%>
                <% break;
                      case QuestionTypeEnum.Alternative:%>
                <%=Html.DropDownList("somename",question.Answers)%>
                <% break;
                  }%>
            </div>
        </div>
        <% } %>
    </div>
    <% } %>
</div>
<div id="submittButton">
    <%=Html.SubmitButton()%></div>
<% } %>

现在我在控制器中需要的是List< ResponseAnswer>,
ResponseAnswer具有以下属性

string questionID,
字符串AnswerText,
bool AnswerBool,
号码AnswerNumber,

那么如何将包含questionID,AnswerType和Answer的未知数量的项目传递给控制器​​.
在webforms中,我通过使用转发器而不是foreach呈现表单来解决此问题,然后通过问题转发器迭代检查控件ID,每个转发器项目包含隐藏的questionid元素和id = AnswerType的输入.
但这会严重打破mvc关注的分离吗?

那么有没有办法让我的控制器接受List< ResultAnswer>并以某种方式构建此列表而不破坏soc,如果没有,我如何将整个formresult传递回控制器,以便我可以在那里而不是在视图中迭代表单数据.

解决方法

加里的回答将起作用(因此,投票).但是,您可以直接将绑定模型建立到列表中,我认为它更优雅一些. There are instructions in this blog post.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...