title: How to bind value from database to Dropdown List in MVC 3
body: <p>I am trying to bind a dropdown list from database in mvc3.</p>
  
  <p>I have two tables.
  <strong>tblEmp:</strong>
  EmpID (pk),
  EName,
  Age,
  Address,
  EmailID,
  DeptID (fk).</p>
  
  <p><strong>tblDept</strong>
  DeptID (pk),
  DeptName,
  DeptHead.</p>
  
  <p>I am trying to bind create an Employee application with the basic details of an employee
  Name, Age, Address, EmailID, and Dept Name. I am trying to bind the Dept Name dropdownlist from the other table.</p>
  
  <p><strong>This is my Model:</strong></p>
  
  <pre><code>namespace MvcEmployeeApplication.Models
  {
  
     public class UandPcompare
  {
      public int EmpID { get; set; }
      public string EName { get; set; }
      public int Age { get; set; }
      public string Address { get; set; }
      public string EmailID { get; set; }
      public int DeptID { get; set; }
      public string DeptName { get; set; }
      public string DeptHead { get; set; }
  
      public IList&lt;SelectListItem&gt; Drp_DeptNames { get; set; }
  }
  }
  </code></pre>
  
  <p><strong>This is Controller:</strong></p>
  
  <pre><code>    [HttpGet]
      public ActionResult Create()
      {
          FillDeptName();        
          return View();
      }
  
      [HttpPost]
      public ActionResult Create(tblEmployee tblEmp)
      {
          test.Entry(tblEmp).State = System.Data.EntityState.Added;
          test.SaveChanges();
          return RedirectToAction("Index");
  
      }
  
      public ActionResult FillDeptName()
      {
          UandPcompare filldeptNme = new UandPcompare();            
          filldeptNme.Drp_DeptNames = (from DptName in test.tblDepts
                                       select new SelectListItem()
                                       {
                                           Text = DptName.DeptName,
                                           Value = SqlFunctions.StringConvert((double)DptName.DeptID)
                                       }).ToList&lt;SelectListItem&gt;();
          return View("Create");
      }
  </code></pre>
  
  <p><strong>This is MyView:</strong></p>
  
  <pre><code>@model MvcEmployeeApplication.Models.UandPcompare
  @{
  ViewBag.title = "Edit";
  }
  &lt;h2&gt; Create &lt;/h2&gt;
  @using (Html.BeginForm())
  {
  &lt;fieldset&gt;
  &lt;legend&gt; Create &lt;/legend&gt;
  
  &lt;div&gt;
    Employee ID:  @Html.DisplayFor(model =&gt; model.EmpID)
  &lt;/div&gt;
  &lt;div&gt;
    Employee Name:  @Html.EditorFor(model =&gt; model.EName)
  &lt;/div&gt;
  &lt;div&gt;
    Email-ID:  @Html.EditorFor(model =&gt; model.EmailID)
  &lt;/div&gt;
  &lt;div&gt;
    Address: @Html.EditorFor(model =&gt; model.Address)
  &lt;/div&gt;
  &lt;div&gt;
    Dept Name: @Html.DropDownList("DeptName", Model.Drp_DeptNames, "Select")
  &lt;/div&gt;
  &lt;p&gt;
  &lt;input type="submit" value="Create" /&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;div&gt;
  @Html.ActionLink("Back to Index", "Index");
  &lt;/div&gt;
  </code></pre>