asp.net-mvc-3 - 在 Razor 中提交 parent 和 child

标签 asp.net-mvc-3 razor submit parent-child

您好,我正在尝试提交父项和子项,我可以提交父项但不能提交子项,有什么办法可以做到这一点吗?

这是我的代码。

@model IECWeb.Models.CurrencyDay

using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>CurrencyDay</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.CurrencyDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.CurrencyDate)
        @Html.ValidationMessageFor(model => model.CurrencyDate)
    </div>

    <p />

    <table>
        <tr>
            <th>Currency</th>
            <th>Rate</th>
        </tr>
        @foreach (var item in Model.Currency) {
            <tr>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => item.Name)
                        @Html.ValidationMessageFor(model => item.Name)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => item.Rate)
                        @Html.ValidationMessageFor(model => item.Rate)
                    </div>
                </td>
            </tr>
        }
    </table>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

当我提交时,我得到了 CurrencyDay 对象,但不是 Currency 列表

谢谢 寿司咬。

最佳答案

我建议您使用编辑器模板而不是在您的 View 中编写循环:

<table>
    <tr>
        <th>Currency</th>
        <th>Rate</th>
    </tr>
    @Html.EditorFor(x => x.Currency)
</table>

然后在将为货币集合的每个元素呈现的相应编辑器模板中(~/Views/Shared/EditorTemplates/CurrencyViewModel.cshtml):

@model CurrencyViewModel
<tr>
    <td>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </td>
    <td>
        <div class="editor-field">
            @Html.EditorFor(model => model.Rate)
            @Html.ValidationMessageFor(model => model.Rate)
        </div>
    </td>
</tr>

请注意,编辑器模板的名称和位置很重要。按照惯例,位置应该是 ~/Views/SomeController/EditorTemplates(如果模板在 Controller 的操作之间重复使用)或更全局的 ~/Views/Shared/EditorTemplates .编辑器模板的名称应该是您正在迭代的集合中每个元素的类型的名称。

关于asp.net-mvc-3 - 在 Razor 中提交 parent 和 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416712/

相关文章:

c# - 如何为多种数据类型创建一个下拉列表?

c# - 构建 MVC3 EditorFor 模板时有没有办法访问 DataAnnotations?

visual-studio - 如何在 VS 中为 Razor 页面 (cshtml) 添加自定义代码片段

razor - 如何在 ServiceStack 中正确本地化 Razor View

c# - 如何实现 "masterpage"级别的逻辑

javascript的submit()不是一个函数?

Javascript - 提交后一分钟无法提交的表单

asp.net-mvc-3 - Razor C# - 设置复选框值

iphone - iTunes Connect 应用描述文本不接受换行符,描述被压缩

asp.net - Azure 和 Aspnet_regsql.exe