title: @Html.DropDownList set selected value dynamic MVC 4 Razor
body: <p>I use my system to create @ ViewBag Html.DropDownList. For normal classes it works. But I have an ICollection in my model that I am creating dynamically @Html.DropDownList but do not know how to set the value of the item and had to try to make the error:</p>
  
  <pre><code>DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Key'.
  </code></pre>
  
  <h3>Controller</h3>
  
  <pre><code>ViewBag.ListaCaracteristica = new SelectList(ListagemPadrao.ListaTipoCaracteristica(), "Key", "Texto");
  </code></pre>
  
  <h3>Model ItemLPesquisa</h3>
  
  <pre><code>public class ItemLPesquisa
  {
      public int Key { get; set; }
      public string Texto { get; set; }
  }
  </code></pre>
  
  <h3>List</h3>
  
  <pre><code>public static IEnumerable&lt;ItemLPesquisa&gt; ListaTipoCaracteristica()
  {
      List&lt;ItemLPesquisa&gt; retorno = new List&lt;ItemLPesquisa&gt;();
      retorno.Add(new ItemLPesquisa { Key = 1, Texto = "Data" });
      retorno.Add(new ItemLPesquisa { Key = 2, Texto = "Texto Curto" });
      retorno.Add(new ItemLPesquisa { Key = 3, Texto = "Número Inteiro" });
      retorno.Add(new ItemLPesquisa { Key = 4, Texto = "Número Decimal" });
      return retorno;
  }
  </code></pre>
  
  <h3>View</h3>
  
  <pre><code>&lt;div class="controls"&gt;
      &lt;ul id="PhonesEditor" style="list-style-type: none"&gt;
      @if (Model.ListaProdutoCaracteristica != null)
      {
          foreach (var item in Model.ListaProdutoCaracteristica)
          {
              &lt;div class="row"&gt;
                  &lt;input type="hidden" name="ListaProdutoCaracteristica[@linha.ToString()].ProdutoPadraoID" value="3"&gt;
                  &lt;input type="hidden" name="ListaProdutoCaracteristica[@linha.ToString()].ProdutoPadraoCaracteristicaID" value="3"&gt;
  
                  &lt;div class="col-md-3"&gt;
                      @Html.LabelFor(m =&gt; item.TipoCaracteristicaID) @Html.ValidationMessageFor(m =&gt; item.TipoCaracteristicaID)
                      @Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", 
                          new SelectList((SelectList)ViewBag.ListaCaracteristica, "Key", "Texto", 2), String.Empty,
                          new { @class = "form-control" })
                  &lt;/div&gt;
  
                  &lt;div class="col-md-7"&gt;
                      @Html.LabelFor(m =&gt; m.Descricao) @Html.ValidationMessageFor(m =&gt; m.Descricao)
                      @Html.TextBoxFor(m =&gt; m.Descricao, new { @class = "form-control" })
                  &lt;/div&gt;                    
              &lt;/div&gt;
              linha++;
          }
      }
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <h3>Error this code</h3>
  
  <pre><code>@Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", 
    new SelectList((SelectList)ViewBag.ListaCaracteristica, "Key", "Texto", 2), String.Empty,
    new { @class = "form-control" })
  </code></pre>