Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve an array in viewmodel
    primarykey
    data
    text
    <p>I'm trying to create a page for editing my attachments.</p> <p>The Attachment Model:</p> <pre><code>public class Attachment { ... private IList&lt;JSONI18NText&gt; titles = new List&lt;JSONI18NText&gt;(); private IList&lt;JSONI18NText&gt; descriptions= new List&lt;JSONI18NText&gt;(); ... public virtual IList&lt;JSONI18NText&gt; Titles { get { return titles; } set { this.titles = value; } } public virtual IList&lt;JSONI18NText&gt; Descriptions { get { return descriptions; } set { this.descriptions= value; } } </code></pre> <p>The JSONI18NText Model:</p> <pre><code>public class JSONI18NText { public int LanguageId { get; set; } public string Text { get; set; } } </code></pre> <p>The Attachment ViewModel:</p> <pre><code>public class AttachmentModel { public AttachmentModel() { } public AttachmentModel(Attachment at) { ... this.Titles = new List&lt;JSONI18NTextModel&gt;(); this.Descriptions = new List&lt;JSONI18NTextModel&gt;(); foreach (JSONI18NText title in at.Titles) { this.Titles.Add(new JSONI18NTextModel(title, "Title")); } foreach (JSONI18NText description in at.Descriptions) { this.Descriptions.Add(new JSONI18NTextModel(description, "Description")); } } [Display(Name = "Title", Description = "Title of the file")] public IList&lt;JSONI18NTextModel&gt; Titles { get; set; } [Display(Name = "Description", Description = "Description of the attachment file")] [DataType(DataType.MultilineText)] public IList&lt;JSONI18NTextModel&gt; Descriptions { get; set; } </code></pre> <p>The JSONI18NText ViewModel:</p> <pre><code>public class JSONI18NTextModel { public JSONI18NTextModel() { } public JSONI18NTextModel(JSONI18NText jsonI18nText) { this.LanguageId = jsonI18nText.LanguageId; this.Text = jsonI18nText.Text; } [HiddenInput(DisplayValue = false)] public int LanguageId { get; set; } public string Text { get; set; } } </code></pre> <p>Now, what I'm trying to achieve is an edit form with a tabbed list for the languages: for example two tabs, one for English and one for Italian, if you click on each tab you read the input value of title and description for that particular language.</p> <p>Everything works like a charm: I used a view script with two partial views, one for the lists and another for the JSONI18NTextModel:</p> <p>Edit.cshtml:</p> <pre><code>... @Html.EditorFor(model =&gt; model.Titles, "EditLabels") @Html.EditorFor(model =&gt; model.Descriptions, "EditLabels") ... </code></pre> <p>EditLabels.cshtml:</p> <pre><code>@model List&lt;CR2.Web.Areas.Admin.Models.JSONI18NTextModel&gt; @using CR2.Web.Infrastructure @using CR2.Web.Areas.Admin.Models @if(Model.Count() == 1) { @Html.EditorFor(model =&gt; model[0], "EditLabel"); } else { for(int i = 0; i &lt; Model.Count(); ++i) { &lt;div&gt; &lt;ul&gt; @Html.EditorFor(model =&gt; model[i], "EditLabel") &lt;/ul&gt; &lt;/div&gt; } } </code></pre> <p>EditLabel.cshtml: </p> <pre><code>@model CR2.Web.Areas.Admin.Models.JSONI18NTextModel @using CR2.Web.Infrastructure &lt;li&gt; @Html.HiddenFor(model =&gt; model.LanguageId) &lt;div&gt; @Html.LabelWithTooltip(model =&gt; model.Text) &lt;/div&gt; &lt;div&gt; @Html.EditorFor(model =&gt; model.Text) @Html.ValidationMessageFor(model =&gt; model.Text) &lt;/div&gt; &lt;/li&gt; </code></pre> <p>When I render it, it builds fields with names like "Titles.[0].Text", which is perfect I think...</p> <p>The problem comes when I submit the form: "Titles" and "Descriptions" aren't populated in the AttachmentModel...(everything else is populated)</p> <p>Any ideas? What am I doing wrong?</p> <p>Thanks a lot!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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