Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 282451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:17:24+00:00 2026-05-12T05:17:24+00:00

I’m having problems binding on a form with multiple models being submitted. I have

  • 0

I’m having problems binding on a form with multiple models being submitted. I have a complaint form which includes complaint info as well as one-to-many complainants. I’m trying to submit the form but I’m getting errors on the bind. ModelState.IsValid always returns false.

If I debug and view the ModelState Errors, I get one saying:
“The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph”.

Also, when debugging, I can see that the the Complaint Model does get populated with Complainants from the form submission, so it seems that part is working.

I’m not sure if what I’m doing is not possible with the default ModelBinder, or if I’m simply not going about it the right way. I can’t seem to find any concrete examples or documentation on this. A very similar problem can be found on stackoverflow here but it doesn’t seem to deal with strongly typed views.

Controller Code:

    public ActionResult Edit(int id)
    {
        var complaint = (from c in _entities.ComplaintSet.Include("Complainants")
                     where c.Id == id
                     select c).FirstOrDefault();

        return View(complaint);
    }

    //
    // POST: /Home/Edit/5
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Complaint complaint)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }
        try
        {
            var originalComplaint = (from c in _entities.ComplaintSet.Include("Complainants")
                                     where c.Id == complaint.Id
                                     select c).FirstOrDefault();
            _entities.ApplyPropertyChanges(originalComplaint.EntityKey.EntitySetName, complaint);
            _entities.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

View Code (This is a partial view that gets called by Create/Edit Views, which are also strongly typed with Complaint):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ProStand.Models.Complaint>" %>

<%= Html.ValidationSummary() %>
<% using (Html.BeginForm()) {%>

<table cellpadding="0" cellspacing="0" class="table">
    <tr>
        <td>
        <label for="DateReceived">Date Received:</label>
            <%= Html.TextBox("DateReceived") %>
            <%= Html.ValidationMessage("DateReceived", "*") %>    
        </td>
        <td>
            <label for="DateEntered">Date Entered:</label>
            <%= Html.TextBox("DateEntered")%>
            <%= Html.ValidationMessage("DateEntered", "*") %>
        </td>
    </tr>
    <tr>
        <td>
            <label for="Concluded">Concluded:</label>
            <%= Html.CheckBox("Concluded")%>
            <%= Html.ValidationMessage("Concluded", "*") %>
            </td>
        <td>
            <label for="IncidentDate">Incident Date:</label>
            <%= Html.TextBox("IncidentDate")%>
            <%= Html.ValidationMessage("IncidentDate", "*") %></td>
    </tr>
</table>
    <hr />
    <table>
    <% if (Model != null) {
           int i = 0;
       foreach (var complainant in Model.Complainants){ %>
       <%= Html.Hidden("Complainants[" + i + "].Id", complainant.Id)%>
    <tr>
        <td>
            <label for="Surname">Surname:</label>

            <%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%>
            <%= Html.ValidationMessage("Surname", "*")%>
        </td>
        <td>
            <label for="GivenName1">GivenName1:</label>
            <%= Html.TextBox("Complainants[" + i + "].GivenName1", complainant.GivenName1)%>
            <%= Html.ValidationMessage("GivenName1", "*")%>
        </td>
    </tr>
    <% i++; %>
    <% }} %>
    <tr>
        <td colspan=2>
            <input type="submit" value="Submit" />
        </td>
    </tr>
</table>
<% } %>
<div>
    <%=Html.ActionLink("Back to List", "Index") %>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:17:24+00:00Added an answer on May 12, 2026 at 5:17 am

    Blind guess:

    change:

    <%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%>
    

    with:

    <%= Html.TextBox("Complaint.Complainants[" + i + "].Surname",  
    complainant.Surname)%>
    

    Respectively – add "Complaint." before "Complainants[…"

    EDIT:

    This is NOT a right answer. Left it undeleted just because that might add some value until proper answer pops up.

    EDIT2:

    I might be wrong, but for me it seems there’s problem with entity framework (or – with the way you use it). I mean – asp.net mvc manages to read values from request but can’t initialize complainants collection.

    Here it’s written:

    The InitializeRelatedCollection(TTargetEntity) method initializes an existing EntityCollection(TEntity) that was created by using the default constructor. The EntityCollection(TEntity) is initialized by using the provided relationship and target role names.

    The InitializeRelatedCollection(TTargetEntity) method is used during deserialization only.

    Some more info:

    Exception:

    • InvalidOperationException

    Conditions:

    • When the provided EntityCollection(TEntity) is already initialized.
    • When the relationship manager is already attached to an ObjectContext.
    • When the relationship manager already contains a relationship with this name and target role.

    Somewhy InitializeRelatedCollection gets fired twice. Unluckily – i got no bright ideas why exactly. Maybe this little investigation will help for someone else – more experienced with EF. 🙂

    EDIT3:
    This isn’t a solution for this particular problem, more like a workaround, a proper way to handle model part of mvc.

    Create a viewmodel for presentation purposes only. Create a new domain model from pure POCOs too (because EF will support them in next version only). Use AutoMapper to map EFDataContext<=>Model<=>ViewModel.

    That would take some effort, but that’s how it should be handled. This approach removes presentation responsibility from your model, cleans your domain model (removes EF stuff from your model) and would solve your problem with binding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 308k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer NavigateUrl simply defines what string will go into the href=""… May 13, 2026 at 9:39 pm
  • Editorial Team
    Editorial Team added an answer You want to use nextAll(). Next is only going to… May 13, 2026 at 9:39 pm
  • Editorial Team
    Editorial Team added an answer You can install WordPress in your subfolder, maybe /wordpress/, then… May 13, 2026 at 9:39 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.