I have a simple Parent Child Model
public class ApplicationTableAndFieldsViewModel
{
[Key]
[Required]
public int ParentTableID { get; set; }
[Required]
public string Description { get; set; }
public List<ApplicationTableField> ApplicationTableFields { get; set; }
}
I have no problem passing the entire model to the view from the controller:
public ViewResult Edit(int parentTableID)
{
ApplicationTableAndFieldsViewModel applicationTable = repository.ApplicationTablesVM
.FirstOrDefault(p => p.ParentTableID == parentTableID);
ViewBag.FieldTypeList = repository.FieldTypes;
IEnumerable<string> FieldTypeDrop = repository.FieldTypes;
List<SelectListItem> selectList = new List<SelectListItem>();
foreach (var c in FieldTypeDrop)
{
SelectListItem i = new SelectListItem();
i.Text = c;
i.Value = c;
selectList.Add(i);
}
ViewBag.FieldTypeList = selectList;
return View(applicationTable);
}
In the view, I have no problem accessing and looping through the children.
foreach (MASTS.Domain.ApplicationTableField f in Model.ApplicationTableFields)
{
<tr>
<td colspan="7">@Html.EditorFor(model => f.Description)</td>
<td colspan="3">@Html.DropDownListFor(model => f.FieldType, new SelectList(ViewBag.FieldTypeList, "Text", "Value"),f.FieldType)</td>
<td colspan="2">@Html.CheckBoxFor(model => f.IsAudited)</td>
</tr>
}
However, when I hit the save button and come back to the Edit Post, the parent data is there, but the child object is null.
Can someone help me understand why that would be or how to fix this?
Thanks,
Ed