I have two classes:
public class bill {
public int billId { get;set;}
public decimal billAmount { get;set;}
...................
public virtual ICollection<lift> Lifts { get;set;}
}
public class lift {
public int liftId { get;set;}
public int billId { get;set;}
......
}
I am accessing the child payments related to existing bills by using the following code in my view:
@for (int i = 0; i < Model.Lifts.Where(x => x.archived != true).ToList().Count; i++)
{
<tr>
<td>@Html.EditorFor(model => model.Lifts.ToList()[i].liftId, new { htmlAttributes = new { @class = "form-control" } })</td>
@Html.ValidationMessageFor(model => model.Lifts.ToList()[i].liftId, "", new { @class = "text-danger" })
<td>@Html.EditorFor(model => model.Lifts.ToList()[i].liftDate, new { htmlAttributes = new { @class = "form-control" } })</td>
@Html.ValidationMessageFor(model => model.Lifts.ToList()[i].liftDate, "", new { @class = "text-danger" })
<td>@Html.EditorFor(model => model.Lifts.ToList()[i].liftAmount, new { htmlAttributes = new { @class = "form-control" } })</td>
@Html.ValidationMessageFor(model => model.Lifts.ToList()[i].liftAmount, "", new { @class = "text-danger" })
<td>@Html.EditorFor(model => model.Lifts.ToList()[i].archived, new { htmlAttributes = new { @class = "form-control" } })</td>
@Html.ValidationMessageFor(model => model.Lifts.ToList()[i].archived, "", new { @class = "text-danger" })
<td>@Html.EditorFor(model => model.Lifts.ToList()[i].archivedDate, new { htmlAttributes = new { @class = "form-control" } })</td>
@Html.ValidationMessageFor(model => model.Lifts.ToList()[i].archivedDate, "", new { @class = "text-danger" })
</tr>
}
This is all working as expected including the field validation on the payments if I remove required fields from the lift class but when I post my model to save the changed bill the Model.Lifts entity is always null
How can I bind the changed lift entities from the related bill to the post of my edit controller for bill, I have tried appending the property to the list of other properties but this has not worked. My controller is as follows:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "billId,.......,Lifts")] bill bill)
{
if (ModelState.IsValid)
{
db.Entry(bill).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(bill);
}