title: MVC Drop Down list with entity framework
body: <p>I've created an MVC project using entity framework code first.  My model is simply a form that gathers information.  </p>
  
  <pre><code>public class Application
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string MiddleInitial { get; set; }
      public string LastName { get; set; }
      public int SSN { get; set; }
      public DateTime DOB { get; set; }
      public string Street { get; set; }
      public string City { get; set; }
      public string State {get; set; }
      public string Zip { get; set; }
      public int HomePhone { get; set; }
      public int BusinessPhone { get; set; }
      public int MobilePhone { get; set; }
  </code></pre>
  
  <p>}</p>
  
  <p>My goal is to create a drop down list with all of the states, but I'm finding this to be very difficult given that I've already created the database via scaffolding with views and controllers.  Is there a simple way to do this and tie it in with the existing database?  I've searched for almost the entire day with no luck.  An overview/explanation of what to include for the model/controller/view would be amazing!</p>
  
  <hr>
  
  <p>Update: I've created a new model named "State" with properties "Id" and "StateName" and have created some states in the database. In my "Application" controller inside the Create action method I have:</p>
  
  <p><strong><em>Controller</em></strong></p>
  
  <pre><code>public ActionResult Create()
      {
  
          ApplicationDbContext db = new ApplicationDbContext();
          this.ViewData["Id"] = new SelectList(db.States.ToList(), "Id", "StateName");
          return View();
      }
  </code></pre>
  
  <p><em>View</em></p>
  
  <pre><code>@Html.DropDownList("Id")
  </code></pre>
  
  <p>Now the problem is I'm getting this error " There is no ViewData item of type 'IEnumerable' that has the key 'Id'." Would really appreciate help!</p>