0

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

Murilo
  • 1,112
  • 1
  • 18
  • 33
ebick
  • 79
  • 10
  • That would be great if you would show Post action. Did you check what names are rendered in those controls in each row? – jgasiorowski Oct 17 '15 at 14:06

1 Answers1

5

I had this problem once. It just worked with for, instead of foreach.

 @for (int i = 0; i < Model.ApplicationTableFields.Count(); i++)
 {
     @Html.EditorFor(model => Model.ApplicationTableFields[i].Description, new { htmlAttributes = new { @class = "form-control" } })
 }
Murilo
  • 1,112
  • 1
  • 18
  • 33
  • This part isn't my problem. It's getting the children BACK in the model on the POST. Thanks anyway. – ebick Oct 17 '15 at 15:05
  • Ok. But you can try it. As I said, once I had problems with saving collections, and I solved it by changing foreach to for. Sounds the same thing, but it's not. Anyways, maybe I didn't get your problem. Good luck – Murilo Oct 17 '15 at 20:41
  • 1
    @ebick, what do you mean _this part isn't my problem_? Of course it is. Your `foreach` loop is generating duplicate `name` attributes that have absolutely no relationship to your model and therefore will never bind to your model (plus generating invalid html because of duplicate `id` attributes). You must use a `for` loop (or custom `EditorTemplate`). Inspect the html your currently generating and compare that with the html generated by a `for` loop to understand your error. –  Oct 17 '15 at 21:44
  • Apologies. When I first looked at this snippet, I thought Murilo was addressing the ability to cycle through the children in the Edit view, and therefore I was dismissive of it as a solution to my issue. I will give it a try. – ebick Oct 19 '15 at 14:09
  • This absolutely WAS the solution. Thanks Murilo. – ebick Oct 19 '15 at 14:36
  • Great to hear that @ebick! So now accept my answer please – Murilo Oct 19 '15 at 19:02
  • Thankyou! so much time wasted on this. This is the answer – codemonkeytony May 09 '19 at 06:30