Note that there are some explanatory texts on larger screens.

plurals
  1. POModel Binding on Multiple Model Form Submission from Strongly-Typed View
    text
    copied!<p>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.</p> <p>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".</p> <p>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.</p> <p>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 <a href="https://stackoverflow.com/questions/711184/asp-net-mvc-example-of-editing-multiple-child-records">here</a> but it doesn't seem to deal with strongly typed views. </p> <p>Controller Code:</p> <pre><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(); } } </code></pre> <p>View Code (This is a partial view that gets called by Create/Edit Views, which are also strongly typed with Complaint):</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;ProStand.Models.Complaint&gt;" %&gt; &lt;%= Html.ValidationSummary() %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;table cellpadding="0" cellspacing="0" class="table"&gt; &lt;tr&gt; &lt;td&gt; &lt;label for="DateReceived"&gt;Date Received:&lt;/label&gt; &lt;%= Html.TextBox("DateReceived") %&gt; &lt;%= Html.ValidationMessage("DateReceived", "*") %&gt; &lt;/td&gt; &lt;td&gt; &lt;label for="DateEntered"&gt;Date Entered:&lt;/label&gt; &lt;%= Html.TextBox("DateEntered")%&gt; &lt;%= Html.ValidationMessage("DateEntered", "*") %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;label for="Concluded"&gt;Concluded:&lt;/label&gt; &lt;%= Html.CheckBox("Concluded")%&gt; &lt;%= Html.ValidationMessage("Concluded", "*") %&gt; &lt;/td&gt; &lt;td&gt; &lt;label for="IncidentDate"&gt;Incident Date:&lt;/label&gt; &lt;%= Html.TextBox("IncidentDate")%&gt; &lt;%= Html.ValidationMessage("IncidentDate", "*") %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;hr /&gt; &lt;table&gt; &lt;% if (Model != null) { int i = 0; foreach (var complainant in Model.Complainants){ %&gt; &lt;%= Html.Hidden("Complainants[" + i + "].Id", complainant.Id)%&gt; &lt;tr&gt; &lt;td&gt; &lt;label for="Surname"&gt;Surname:&lt;/label&gt; &lt;%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%&gt; &lt;%= Html.ValidationMessage("Surname", "*")%&gt; &lt;/td&gt; &lt;td&gt; &lt;label for="GivenName1"&gt;GivenName1:&lt;/label&gt; &lt;%= Html.TextBox("Complainants[" + i + "].GivenName1", complainant.GivenName1)%&gt; &lt;%= Html.ValidationMessage("GivenName1", "*")%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% i++; %&gt; &lt;% }} %&gt; &lt;tr&gt; &lt;td colspan=2&gt; &lt;input type="submit" value="Submit" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload