title: ASP.NET MVC 4 Want to populate dropdown list from database
body: <p>I am new guy in ASP.NET MVC 4. I want to populate dropdownlist from database table BO where Column name is <code>Id, Code, Name, OrgId</code>. I want to bind two <code>Code</code> &amp; <code>Name</code>column's data to DataTextfield and <code>Id</code> column Data to DataValueField of dropdown. I have created code for this  which are as follows BUT ITS NOT RETURNING DATA FROM TABLE and <code>var BOList</code> is remain empty : </p>
  
  <pre><code>my connectionstring is 
  
  
   &lt;add name="iRegDBContext"
    connectionString="Data Source=****;Initial Catalog=iReg;User ID=**;Password=****;Integrated Security=True"
    providerName="System.Data.SqlClient"
  /&gt;
  </code></pre>
  
  <p>My Controller class : </p>
  
  <pre><code>public class iRegController : Controller
  {
      private iRegDBContext l_oDbBO = new iRegDBContext();  
  
      // GET: /iReg/
  
      public ActionResult PopulatejQgrid()
      {
          var BOList = l_oDbBO
                       .BO
                       .ToList()
                       .Select(d =&gt; new SelectListItem
                           {
                               Value = d.Id.ToString(),
                               Text = d.Name + "[ " + d.Code + " ]"
                           });
          ViewBag.BOData = new SelectList(BOList, "Value", "Text"); 
          return View();
      }
  }
  </code></pre>
  
  <p>My Model class :</p>
  
  <pre><code>public class BO
  {
      public Guid Id { get; set; }
      public string Code { get; set; }
      public string Name { get; set; }
  }
  
  public class iRegDBContext : DbContext
  {
      public DbSet&lt;BO&gt; BO { get; set; }
  }
  </code></pre>
  
  <p>My cshtml class :</p>
  
  <pre><code>@model MvciReg.Models.BO
  
  @{
      ViewBag.Title = "PopulatejQgrid";
  }
  
  @using (Html.BeginForm())
  {
      &lt;fieldset&gt;
          BO : 
  
          @Html.DropDownList("BOData")
          &lt;p&gt;
              &lt;input type="submit" value="Go" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p><strong>I really don't know where I am going wrong. I developed my code from reference of following link</strong> <a href="http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/%22here%22" rel="nofollow">Click here </a>. <strong>Kindly suggest correction in code ...</strong></p>
  
  <p><strong>UPDATE:</strong> I tried following <strong>Matt Bodily's code</strong> in my controller and what I see is code is not fetching data from database and that code is </p>
  
  <pre><code>    public ActionResult populatejQgrid()
      {
          ViewBag.BOData = GetDropDown();
  
          return View();
      }
  
      public static List&lt;SelectListItem&gt; GetDropDown()
      {
          List&lt;SelectListItem&gt; ls = new List&lt;SelectListItem&gt;();
          var lm = from m in db.BOs //fetch data from database
                   select m;
          foreach (var temp in lm)
          {
              ls.Add(new SelectListItem() { Text = temp.Name, Value = temp.Id.ToString() });
          }
          return ls;
      }
  </code></pre>
  
  <p>In Controller : </p>
  
  <pre><code>      @Html.DropDownList("BOData", (List&lt;SelectListItem&gt;)ViewBag.BOData)
  </code></pre>
  
  <p>But when I saw value of <code>ls</code> through watch it always show me <code>Count = 0</code> but its not giving me any error.</p>
  
  <p><strong>I found something new this problem. When I kept mouse pointer over var lm; it shows me query and in query table name in FROM clause is not that one in my SQL database. My SQL table name is BO and in query it is taking BOes. I don't know from where this name is coming. I think this is the main cause of all this problem So How I overcome this??</strong>   </p>