Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to POST a form containing Editor Templates for an ICollection?
    primarykey
    data
    text
    <p>I'm working with ASP.NET MVC 4 and I have the following models:</p> <pre><code>public class MultiLanguageText { [Key] public int Id { get; set; } [Required] public string Name { get; set; } public virtual ICollection&lt;LocalizedText&gt; LocalizedTexts { get; set; } } public class LocalizedText { [Key] public int Id { get; set; } [ForeignKey("Language")] public int LanguageId { get; set; } public virtual Language Language { get; set; } [ForeignKey("MultiLanguageText")] public int MultiLanguageTextId { get; set; } public virtual MultiLanguageText MultiLanguageText { get; set; } public string Text { get; set; } } </code></pre> <p>I'm trying to create an Edit view for <code>MultiLanguageText</code> which will also contain an editor for every item in the <code>LocalizedTexts</code> property. The form goes like this:</p> <pre><code>@using (Html.BeginForm("Edit", "MultiLanguageText", FormMethod.Post)) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;MultiLanguageText&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Id) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; @Html.EditorFor(model =&gt; model.LocalizedTexts) &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre> <p>The editor template I've created (in the file <code>~/Views/Shared/EditorTemplates/LocalizedText.cshtml</code>) is the following:</p> <pre><code>&lt;fieldset&gt; &lt;legend&gt;LocalizedText&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Id) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.LanguageId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.LanguageId) @Html.ValidationMessageFor(model =&gt; model.LanguageId) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.MultiLanguageTextId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.MultiLanguageTextId) @Html.ValidationMessageFor(model =&gt; model.MultiLanguageTextId) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Text) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Text) @Html.ValidationMessageFor(model =&gt; model.Text) &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>I'm testing with some pre-existing values and all the data is shown properly in the parent and in the child editors, when the page is shown. However, when I submit the form, all changes done inside any of the child editors are ignored.</p> <p>My <code>HttpPost</code> method is this:</p> <pre><code> [HttpPost] public ActionResult Edit(MultiLanguageText multilanguagetext) { if (ModelState.IsValid) { db.Entry(multilanguagetext).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(multilanguagetext); } </code></pre> <p>My question is: <strong>How do I make it so that all values are posted, from both the parent model and its children model editors?</strong> (I've seen that other similar questions exist, but I've tried their solutions and they didn't seem to fix my problem.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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