in Education by
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);
Sheet Details
Sheet Desc. @Html.TextBoxFor(x => x.Description, new { size = "50" })
Quantity Required @Html.EditorFor(x => x.Quantity)
Tasks
@Html.EditorFor(x => x.QuoteTasks)
Labour Group Task Description Total Hrs Rate Cost
@Html.ValidationSummary() } And the EditorTemplate is: @model Ornavi.Models.QuoteTask @Html.EditorFor(x => Model.LabourGroup) @Html.EditorFor(x => Model.Description) @Html.EditorFor(x => Model.TotalHours) @Html.EditorFor(x => Model.Rate) @Html.HiddenFor(x => Model.ID) 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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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: Don't use your EF classes as viewmodels but create own viewmodel classes to pass the data between controller and view. 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.

Related questions

0 votes
    I am developing a .NET core application with Identity login which provides a Bootstrap form In the /Identity ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Part of the series of controls I am working on obviously involves me lumping some of them together in to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    I have a very simple operation where I try to add a HtmlGenericControl to another HtmlGenericControl : protected ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    The ASP.NET application that I am currently responsible for at my day job has hit its limit in terms ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    The ASP.NET application that I am currently responsible for at my day job has hit its limit in terms ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I write the following code to access the page but this is not working for me if (User.Identity. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I write the following code to access the page but this is not working for me if (User.Identity. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a domain model with an aggregate root: public interface IAggregateRoot { public string Id { get; } ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I need to integrate IDX MLS into a real estate website. Also I want to know how to implement ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I am refactoring some CSS on a website. I have been working on, and noticed the absence of traditional ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I am refactoring some CSS on a website. I have been working on, and noticed the absence of traditional ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I'm currently looking at ways to allow people to select multiple files at once to batch upload images. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    When using ASP.NET protected configuration, how can I encrypt the config with just the public key? I can ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a bunch of action methods inside my controller each with its own model binded as input parameter es ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Can using Modules or Shared/Static references to the BLL/DAL improve the performance of an ASP.NET website? ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
...