私は以下の仮説に似た問題があります。私は別のビューに複数回部分ビューを追加しましたが、ビューモデルなどに正しくバインドさせる方法がわかりません。また、単一のfooNameが間違っていると、単一のfooNameに対して検証が行われるようです。私はあなたが見ることができるように、ビューバックにインデックスを追加しましたが、私は実際にそれをどのように使用するかは分かりません。MVCの作成ビューでリスト<object>を正しくバインドするには
注:MVC5.2
ビューモデル
public class Thing
{
public String thingName { get; set; }
public List<Foo> Foos { get; set; }
}
public class Foo
{
public String fooName { get; set; }
}
Fooのビュー
@model Project.Models.Foo
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.fooName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.fooName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fooName, "", new { @class = "text-danger" })
</div>
</div>
</div>
シングビュー
を使用して@model Project.Models.Thing
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.thingName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.thingName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.thingName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="add-foo">
@* at some point add more foos with ajax, for now k.i.s.s. *@
@Html.Partial("/Views/Foo/Create.cshtml", new ViewDataDictionary { { "id", 1 } })
@Html.Partial("/Views/Foo/Create.cshtml", new ViewDataDictionary { { "id", 2 } })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
おそらく関連:http://stackoverflow.com/questions/29486946/asp-net-mvc-parent-child-view-with-parent-view-updating-children –
新しいアイテムをいくつかのオプションについては、[この回答](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)を参照してください。 –