The title of the Q says it all, I've read many posts and still cannot get my head around this. All I'm trying to do is Post
an object with nested objects. The core object populates fine but the nested objects always returns null.
The backend/ViewModel as such:
public class EntityExtendedProperty
{
public ExtendedPropertyDefinition Definition { get; set; }
//Type above contains Definition.ExtendedPropertyOptions
}
The Controller:
public ActionResult EditExtendedProperty(EntityExtendedProperty vm)
{
//Can I access View Model here?
var v = vm; // I debug here
}
The Front-End:
@using (Html.BeginForm("EditExtendedProperty","Organisation",FormMethod.Post, new {vm = Model }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => Model.Definition.Id)
<div class="form-horizontal">
<h4>Edit Extended Property</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<table border="1" cellpadding="10">
<tr>
<td>Area :</td>
<td>@Model.Definition.ExtendedPropertyEntityType.Name</td>
</tr>
<tr>
<td>Title</td>
<td>@Html.EditorFor(m => m.Definition.Title)</td>
</tr>
<tr>
<td>IsEnumerated</td>
<td>@Html.EditorFor(m => m.Definition.IsEnumerated)</td>
@if(Model.Definition.ExtendedPropertyOptions.Any())
{
<td>
<table>
<tr><th>Options</th></tr>
@foreach(var o in Model.Definition.ExtendedPropertyOptions)
{
<tr><td>@Html.EditorFor(m => o.Title)</td></tr>
}
</table>
</td>
}
</tr>
<tr>
<td>AllowMultiSelect</td>
<td>@Html.EditorFor(m => m.Definition.AllowMultiSelect)</td>
</tr>
<tr>
<td>IsDate</td>
<td>@Html.EditorFor(m => m.Definition.IsDate)</td>
</tr>
</table>
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
As you can see, this is all relatively straight forward but why is ExtendedPropertyOptions
values returning null?
Any help greatly appreciated!
答案 0 :(得分:2)
your editor should look like:
@for(var i = 0; i < Model.Definition.ExtendedPropertyOptions.Count; i++)
{
<tr><td>@Html.EditorFor(m => m.Definition.ExtendedPropertyOptions[i].Title)</td></tr>
}
答案 1 :(得分:0)
确保发布的数据在Chrome网络标签中看起来是正确的 - 我认为它应该看起来像
Definition.Title: "Hello"
Definition.ExtendedPropertyOptions[0].Property: "a"
Definition.ExtendedPropertyOptions[1].Property: "b"
您可能必须使用其他方式将html前缀传递给子对象或使用该视图和部分视图来设置TemplateInfo.HtmlFieldPrefix