title: Select data from database and insert into dropdownlist - MVC 4
body: <p>I'm trying to select data from my database to insert into my dropdown list. My problem is that the dropdown list shows nothing and I don't receive any errors. Can someone give a hand with this please?</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public int id { get; set; }
  public string name{ get; set; }
  public IEnumerable&lt;SelectListItem&gt; isto { get; set; }
  
   public ListagemEmpresas()
  {
      isto = GetCompanies();
  }
  
  public SelectList GetCompanies()
  {
      var list = new List&lt;SelectListItem&gt;();
      string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  
      using (var con = new SqlConnection(connection))
      {
          con.Open();
          using (var command = new SqlCommand("SELECT * FROM Companieslist", con))
          {
              SqlDataReader reader = command.ExecuteReader();
              while (reader.Read())
              {
                  string id = reader[0] as string;
                  string name = reader[1] as string;
                  list.Add(new SelectListItem() { Text = name, Value = id });
              }
          }
          con.Close();
      }
      return new SelectList(list, "Value", "Text");
  }
  
  public class DefaultConnection : DbContext
  {
      public DbSet&lt;CompanyList&gt; abc{ get; set; }
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>var dba = new DefaultConnection();
          var query = dba.abc.Select(c =&gt; new SelectListItem
          {
              Value = SqlFunctions.StringConvert((double)c.id),
              Text = c.name,
  
          });
          var model = new CompanyList
          {
              isto = query.AsEnumerable()
          };
  
          return View(model);
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>@model MyProject.Models.CompanyList
  
  @Html.DropDownListFor(m =&gt; m.name, Model.isto, "--Select One--")
  </code></pre>