【问题标题】:Bind collection of complex child objects to view MVC3绑定复杂子对象的集合查看MVC3
【发布时间】:2012-06-03 05:46:41
【问题描述】:

首先,这与Collection of complex child objects in Asp.Net MVC 3 application? 非常相似,但没有一个答案真正为我完全回答了这个问题。

我有一个包含 PollItem 模型列表的 Poll 模型。我的观点是创建新的民意调查,因此传递给视图的模型是完全空的。所以有基本投票信息的文本框,然后是用户可以添加/删除任意数量的投票项目的下部。我需要以某种方式将这些投票项文本框绑定到 PollItems 列表。

@using (Html.BeginForm("CreatePoll", "Polls", FormMethod.Post, new { enctype = "multipart/form-data", id = "createPollForm", @class = "cmxform group" }))
    {
        @Html.TextBoxFor(x => x.Question, new { @class = "title-input full-width prominent", placeholder = "Enter Question Here" })
        <ul class="poll-info">
            <li>
                <p><label>Poll Opens</label></p>
                @Html.TextBoxFor(x => x.OpeningDate, new { id = "openingDatePicker" })
            </li>
            <li>
                <p><label>Poll Closes</label></p>
                @Html.TextBoxFor(x => x.ClosingDate, new { id = "closingDatePicker" })
            </li>
        </ul>
        <section class="description">
            <p><label>Description</label></p>
            @Html.TextAreaFor(x => x.Description, new { @class = "full-width", placeholder = "Enter a more detailed description of the poll here." })
        </section>

        <a class="button"><i class="ico"></i> Attach an object</a>

        <section class="module-questions">
            <ul>
            </ul>
            <span class="button link" onclick="addAnswer()"><i class="ico"></i> Add Answer</span>
        </section>
    }

addAnswer() 函数将列表项中的新文本框附加到将绑定到 PollItem.Description 的无序列表中,这些文本框保存在列表 Poll.PollItems 中。除了描述之外,PollItem 中可能还会有一些其他元素。

我什至不知道从哪里开始,因为我不知道如何绑定动态添加和删除的文本框。

【问题讨论】:

    标签: asp.net asp.net-mvc-3 entity-framework


    【解决方案1】:
    【解决方案2】:

    我建议您查看following blog post,它说明了如何实现这一目标。它演示了自定义 Html.BeginCollectionItem 助手的用法,该助手负责为动态添加和删除的输入字段生成正确的名称。

    【讨论】:

    • 我之前读到的时候不知何故错过了有关动态添加和删除项目的内容。现在我觉得很傻。它最终完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多