用剃须刀提交父母和孩子

sus*_*ite 2 submit parent-child razor asp.net-mvc-3

嗨,我想提交父母和孩子,我能够提交父母罚款,但不是孩子,有没有办法做到这一点?

这是我的代码.

@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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢sushiBite.

Dar*_*rov 5

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

<table>
    <tr>
        <th>Currency</th>
        <th>Rate</th>
    </tr>
    @Html.EditorFor(x => x.Currency)
</table>
Run Code Online (Sandbox Code Playgroud)

然后在相应的编辑器模板中,该模板将为货币集合(~/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>
Run Code Online (Sandbox Code Playgroud)

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