Question

I have an Entity (QuoteSheet) that contains a child entity (QuoteTask), which is loaded using the EntityFramework. However, I am receiving an error when I submit this form.

I have created an edit page for the QuoteSheet entity, which then uses an EditorTemplate to edit the QuoteTask child entity.

The controller code is as follows:

public ActionResult TestEdit(int Id)
{
    var quote = DataContext.QuoteSheets.Where(x => x.ID == Id).FirstOrDefault();
    return View(quote);
}

[HttpPost]
public ActionResult TestEdit(Models.QuoteSheet quote)
{
    return View(quote);
}

A stripped down version of the view is as follows:

@using (Html.BeginForm())
{ 
@Html.ValidationSummary(true)
@Html.HiddenFor(x => x.JobID);

<div class="sectionHeader">Sheet Details</div>
<div class="sectionContent">
    <table>
        <tr>
            <td width="150">Sheet Desc.</td><td>@Html.TextBoxFor(x => x.Description, new { size = "50" })</td>
        </tr>
        <tr>
            <td>Quantity Required</td><td>@Html.EditorFor(x => x.Quantity)</td>
        </tr>
    </table>
</div>

<div class="sectionHeader">Tasks</div>
<div class="sectionContent">
    <table id="Tasks">
        <tr>
            <th>Labour Group</th>
            <th>Task Description</th>
            <th>Total Hrs</th>
            <th>Rate</th>
            <th>Cost</th>
        </tr>
        @Html.EditorFor(x => x.QuoteTasks)
    </table>
    <input type="button" name="AddTasks" id="AddTasks" value="Add" />
</div>

<input type="submit" value="Submit" />

@Html.ValidationSummary()
}

And the EditorTemplate is:

@model Ornavi.Models.QuoteTask



<tr>
    <td>@Html.EditorFor(x => Model.LabourGroup)</td>
    <td>@Html.EditorFor(x => Model.Description)</td>
    <td>@Html.EditorFor(x => Model.TotalHours)</td>
    <td>@Html.EditorFor(x => Model.Rate)</td>
    <td>@Html.HiddenFor(x => Model.ID)</td>
</tr>

When I submit the form, I am getting the following error:

The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.

This only occurs when I use the EditorTemplate - if I remove the editor template and just submit the main entity, it works fine.

I have placed a breakpoint in the [httppost] TestEdit function, but the exception occurs before it reaches this point.

Any ideas on how to successfully use an EditorTemplate to edit a child entity?

Was it helpful?

Solution

The problem is, that the default modelbinder tries to instantiate your EF class and set the navigation properties when binding the form data to your parameter types.

See some similar questions like this one.

You have two options:

  1. Don't use your EF classes as viewmodels but create own viewmodel classes to pass the data between controller and view.

  2. Don't bind directly to the EF class in your Edit controller action but use a FormCollection parameter and bind yourself with UpdateModel as shown in the linked question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top