MVC模型绑定 - 如何通过一个列表作为参数

MVC模型绑定 - 如何通过一个列表作为参数

问题描述:

我有一个MVC问卷应用程序,通过一组问题MVC模型绑定 - 如何通过一个列表作为参数

<% foreach (var question in Model.CriteriaQuestionList) 
    { %> 
<fieldset> 
    <legend>   
     <%= Html.Encode(question.Description)%>   
    </legend> 
</fieldset> 
<%} %> 

我要提交的文本响应列表,使用模型绑定每个问题循环并且正在考虑这种方法

<% using (Html.BeginForm("Index", "ChampionCriteria", FormMethod.Post, new { @class = "themeform" })) 
    { %> 
<% int i = -1; //index %> 
<% foreach (var question in Model.CriteriaQuestionList) 
    { %> 
<fieldset> 
    <legend> 
     <% i = i + 1; %> 
     <%= Html.Encode(question.Description)%> 
     <input name="[<%=i%>].freeTextResponse" class="textarea" type="text" id="text<%=i%>" /> 
    </legend> 
</fieldset> 
<%} %> 

因此,此视图可以使用模型绑定提交问题响应列表。

我可以使用html.textboxfor这种方法吗?

看看here为列表的模型。如果你有可变长度,你可以看看here。你也可以看看Knockout.js的另一种做法。

+0

+1 - 桑德森一个人会很适合在OP的情况下,我认为 – 2012-02-15 19:45:01

+0

好的谢谢。然而,对于前进的方式,我需要能够使用HTML.TextBoxFor和HiddenFor,以便我可以使用服务器端验证。我不想使用Javascript。 – DreamToCode 2012-02-22 21:00:08