【问题标题】:MVC model binding - how to pass a List as a parameterMVC 模型绑定 - 如何将 List 作为参数传递
【发布时间】:2012-02-16 02:54:44
【问题描述】:

我有一个 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 吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    查看here 列表的模型投标。如果你有可变长度,你可以看看here。您也可以查看Knockout.js 了解另一种方法。

    【讨论】:

    • +1 - 我认为 Sanderson 很适合 OP 的情况
    • 好的,谢谢。但是,为了向前迈进,我需要能够使用 HTML.TextBoxFor 和 HiddenFor,这样我才能使用服务器端验证。我不想使用 Javascript。
    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多