C# ASP.NET MVC 4.5在表单提交时将局部视图映射到主视图

C# ASP.NET MVC 4.5在表单提交时将局部视图映射到主视图,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,你好,强大的飞越者 我目前正在开发一个ASP.NETMVC4.5应用程序提交创建表单时,我需要将输入值从局部视图映射到主视图模型。 在我的视图“Create.cshtml”中,我称部分视图为“\u SwotPart.cshtml”。我将ViewModel的一部分传递给局部视图,如下所示: Create.cshtml: @model MyCoolApp.BLL.Models.MainVm @foreach(Model.Swots中的var swot) { @foreach(swot.swotP

你好,强大的飞越者

我目前正在开发一个ASP.NETMVC4.5应用程序提交创建表单时,我需要将输入值从局部视图映射到主视图模型。

在我的视图“Create.cshtml”中,我称部分视图为“\u SwotPart.cshtml”。我将ViewModel的一部分传递给局部视图,如下所示:

Create.cshtml:

@model MyCoolApp.BLL.Models.MainVm

@foreach(Model.Swots中的var swot)
{
@foreach(swot.swotPart中的var swotPart)
{
@Html.Partial(“~/Views/Shared/_SwotPart.cshtml”,SwotPart)
}
}

我的局部视图如下所示,_SwotPartial.cshtml:

<td class="form-group">
    @Html.TextAreaFor(model => model.Label, htmlAttributes: new { Name = nameField, ID = nameField, @class = "form-control", placeholder = Model.SwotTypeId.GetLabel() })
</td>

@Html.TextAreaFor(model=>model.Label,htmlAttributes:new{Name=nameField,ID=nameField,@class=“form control”,placeholder=model.SwotTypeId.GetLabel()})
无论如何,当我提交表单时,部分视图中的值永远不会到达控制器

你对如何正确地绘制地图有什么想法吗


谢谢

问题在于您当前试图实现这一目标的方式将生成的输入名称。Razor需要整个列表的上下文,或者至少是项目在其中的位置,以便生成正确的输入名称。换句话说,解决问题的最简单方法(附带警告)是:

然后,您可以简单地逐个迭代每个列表,这些值将自然地发回相应的列表

但是,如果您使用partial来呈现特定类类型的字段,那么最好创建一个编辑器模板。如果只需将部分代码移动到视图:
Views\Shared\EditorTemplates\SwotPartVm.cshtml
,那么在主视图中,您只需执行以下操作:

@for (var i = 0; i < Model.Swots.Count(); i++)
{
    ...

    <tr>
        <th class="swot-heading">Internal</th>
        @Html.EditorFor(m => m.Swots[i].InternalSwotParts)
    </tr>
    <tr>
        <th class="swot-heading">External</th>
        @Html.EditorFor(m => m.Swots[i].ExternalSwotParts)
    </tr>
}

注意:在您的
SwotVm.cshtml
编辑器模板中,您将只包含单个
SwotVm
的代码。换句话说,不包括
for
语句。

问题在于将以您当前尝试的方式生成的输入名称。Razor需要整个列表的上下文,或者至少是项目在其中的位置,以便生成正确的输入名称。换句话说,解决问题的最简单方法(附带警告)是:

然后,您可以简单地逐个迭代每个列表,这些值将自然地发回相应的列表

但是,如果您使用partial来呈现特定类类型的字段,那么最好创建一个编辑器模板。如果只需将部分代码移动到视图:
Views\Shared\EditorTemplates\SwotPartVm.cshtml
,那么在主视图中,您只需执行以下操作:

@for (var i = 0; i < Model.Swots.Count(); i++)
{
    ...

    <tr>
        <th class="swot-heading">Internal</th>
        @Html.EditorFor(m => m.Swots[i].InternalSwotParts)
    </tr>
    <tr>
        <th class="swot-heading">External</th>
        @Html.EditorFor(m => m.Swots[i].ExternalSwotParts)
    </tr>
}

注意:在您的
SwotVm.cshtml
编辑器模板中,您将只包含单个
SwotVm
的代码。换句话说,不包括
for
语句。

以便应用程序解析发布的值并正确地将其绑定到视图模型。已发布表单数据的名称需要类似

SWOT[x1]。swotParts[x2]。标签

其中x1是一个从0到swot的数字。 其中x2是swot中每个swot部分的0到0之间的数字

现在,当您发布时,表单数据名称只是标签

而不是:

 @Html.TextAreaFor(model => model.Label, htmlAttributes: new { Name = nameField, ID = nameField, @class = "form-control", placeholder = Model.SwotTypeId.GetLabel() })
尝试:

@Model.Label
别忘了用数字替换x1和x2

您可以在此处阅读有关集合的模型绑定的更多信息


以便应用程序解析发布的值并正确地将其绑定到视图模型。已发布表单数据的名称需要类似

SWOT[x1]。swotParts[x2]。标签

其中x1是一个从0到swot的数字。 其中x2是swot中每个swot部分的0到0之间的数字

现在,当您发布时,表单数据名称只是标签

而不是:

 @Html.TextAreaFor(model => model.Label, htmlAttributes: new { Name = nameField, ID = nameField, @class = "form-control", placeholder = Model.SwotTypeId.GetLabel() })
尝试:

@Model.Label
别忘了用数字替换x1和x2

您可以在此处阅读有关集合的模型绑定的更多信息


感谢Chris的详细解释!感谢Chris的详细解释!
@Html.EditorFor(m => m.Swots)
 @Html.TextAreaFor(model => model.Label, htmlAttributes: new { Name = nameField, ID = nameField, @class = "form-control", placeholder = Model.SwotTypeId.GetLabel() })
<textarea name="swots[x1].swotParts[x2].label" class="form-control" placeholder="@Model.SwotTypeId.GetLabel()" >@Model.Label</textarea>