我有一个简单的网络应用程序与以下内容 模型 </跨度> :
公共类SillyModel{ public SillyModel() {Id = Guid.NewGuid();儿童=新名单&lt; SillyModel&gt;(); } [键] public virtual Guid Id {get;组; } public virtual string Value {get;组; } 公共虚拟列表&lt; SillyModel&gt;孩子们{得到;组; }}
}
我有一个编辑视图:
@ 模型 </跨度> WebApplication1.Models.SillyModel
你必须重新构建你编码的方式
Views/YourController
EditorTemplates
SillyModel
EditPartial
foreach
for
的 代码 强>
〜/查看/ YourController / EditorTemplates / SillyModel.cshtml
@model WebApplication1.Models.SillyModel @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.Id) <div class="form-group"> @Html.LabelFor(model => model.Value, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Value, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Value, "", new { @class = "text-danger" }) </div> @for (var index=0; index<Model.Children.Count;index++) { @Html.EditorFor(model=>Model.Children[index]) } </div>
〜/查看/ YourController /编辑
代替 @Html.Partial("EditPartial", Model) , 使用 @Html.EditorFor(m=>m)
@Html.Partial("EditPartial", Model)
@Html.EditorFor(m=>m)
的 说明 强>
EditorTemplates/SillyModel
@Html.EditorFor(model=>Model.Children[index])
indexed
Binding
Model
希望对你有帮助