I have a parent view model (Let's call it ParentViewModel) which has a list of children view models (Let's call them ChildViewModel). Each child view model can be edited independently and I have a separate form which I display in a loop. This works brilliantly but I cannot work out how to post just the child model and ignore the parent.
This is my form:
@model ParentViewModel
...
@foreach (var child in Model.Children)
{
@using (Html.BeginForm("_EditChild", "Admin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.EditorFor(model => child.Content, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => child.Content, "", new {@class = "text-danger"})
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Create" class="btn btn-default new-post" />
</div>
</div>
}
}
And this is the signature of my controller. It is expecting a type ChildViewModel which exists in ParentViewModel as a list.
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _EditPost([Bind(Include = "")] ChildViewModel childViewModel)
{
}
The form works and submits BUT ChildViewModel is null when it reaches the submit controller. This is certainly because binding between the Form Post and the Action is not happening.