title: Setting selected values in a Listbox from the model in .net mvc
body: <p>I am trying to bind my model to Multiple Select in .net MVC. This is my model</p>
  
  <pre><code>public class RuoloProgetto
  {
      [Key]
      public int id_ruolo_prog { get; set; }
      [Display(Name = "Ruolo")]
      public int id_ruolo { get; set; }
      [ForeignKey("id_ruolo")]
      public virtual Ruolo ruolo { get; set; }
      public int id_progetto { get; set; }
      //[ForeignKey("id_progetto")]
      //public virtual Progetto progetto { get; set; }
      [Display(Name = "Utente")]
      public string id_utente { get; set; }
      [ForeignKey("id_utente")]
      public virtual IdentityUser utente { get; set; }
      [DisplayName("Carico di lavoro in %")]
      [Required]
      public int percentuale { get; set; }
      public int versione { get; set; }
      [DisplayName("Moduli Responsabile")]
      public int[] Choices { get; set; }
      public  virtual List&lt;ModelRP&gt; moduli { get; set; }
  }
  </code></pre>
  
  <p>This is in my controller function that sets the values for the selectlist:</p>
  
  <pre><code> List&lt;SelectListItem&gt; specifiche = new List&lt;SelectListItem&gt;();
          var specifiche2 = db.Specificha.Where(p =&gt; p.id_progetto == Progetti.id_progetto).Select(x =&gt; new { x.id_specifiche, x.desc_specifiche }).ToList();
  
          foreach (var item in specifiche2)
          {
              specifiche.Add(new SelectListItem { Value = item.id_specifiche.ToString(), Text = item.desc_specifiche });
          }
          ViewData["Specs"] = specifiche;
  </code></pre>
  
  <p>and this is in my view:</p>
  
  <pre><code> @Html.ListBoxFor(m =&gt; item.Choices,
       new MultiSelectList((IEnumerable&lt;SelectListItem&gt;)ViewData["Specs"], "Value", "Text",
       item.moduli.Select(fl =&gt; new SelectListItem
       {
           Text = fl.moduloutente.desc_specifiche.ToString(),
           Value = fl.moduloutente.id_specifiche.ToString(),
           Selected = item.moduli.Any(y =&gt; y.id_specifiche == fl.id_specifiche)  
       })),
       new
       {
           Selected = true,
           @Class = "js-example-basic-multiple form-control",
           Multiple = "multiple",
           @Name = "progetto.ruoloprogetto[" + index + "].Choices",
           @Id = "progetto.ruoloprogetto[" + index + "]_Choices",
           @Placeholder = "Moduli",
       })
  </code></pre>
  
  <p>The ViewData comes ok! The problem is with the selected values! When I check in immediate window item.moduli is correctly filled with the data, but nothing appears as selected in the interface!</p>
  
  <p>Can someone see what I'm doing wrong? </p>