title: How to Populate a DropDown List using Razor and MVC?
body: <p>I am trying to populate &amp; bind a drop down list control in MVC using Dapper, and I am having trouble finding a good example online. I can find plenty of examples of how to bind a statically defined drop down to a update a database (Like "Male" or "Female") and can get that to work. I can also find many examples of populating a drop down from a database, but I cannot get them to work in conjunction. </p>
  
  <p>I just want my ProjectManifest/Create form to have a drop down populated with my current Projects.</p>
  
  <p>I wish I was better at asking this question, but I do not know what I am doing wrong. I have tried dozens of variations in my code and different tutorials and questions on StackOverflow. The errors I receive from Visual Studio are so unhelpful, they are usually just NullReference exceptions, and there are so many variables in the HTML Helper function call I do not even know what is null.</p>
  
  <p>These are some of the articles I have tried to use to help understand my problem:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/39092995/populate-a-dropdown-list-in-mvc-view">Populate a Dropdown List in MVC View</a></li>
  <li><a href="https://stackoverflow.com/questions/1676840/user-html-helper-to-populate-a-dropdown-list">User HTML Helper to populate a DropDown list</a></li>
  <li><a href="https://stackoverflow.com/questions/22680342/value-cannot-be-null-parameter-name-entityset">Value cannot be null. Parameter name: entitySet</a></li>
  <li><a href="https://www.youtube.com/watch?v=k9CJmLbnFko" rel="nofollow noreferrer">https://www.youtube.com/watch?v=k9CJmLbnFko</a></li>
  <li><a href="http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx" rel="nofollow noreferrer">http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx</a></li>
  <li><a href="https://stackoverflow.com/questions/11226780/binding-data-to-dropdownlist-mvc-razor">Binding Data To DropDownList MVC Razor</a></li>
  <li><a href="https://stackoverflow.com/questions/4833652/asp-net-mvc-with-drop-down-list-and-selectlistitem-assistance">Asp.Net MVC with Drop Down List, and SelectListItem Assistance</a></li>
  </ul>
  
  <p>I think the problem is in my index file, I do not understand Lambda expressions or what the expectation is to be in the arguments for the Html helper "DropDownListFor" or "DropDownList"</p>
  
  <p>Index view</p>
  
  <pre><code>@using (Html.BeginForm("Index", "ProjectManifests", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      ....
      @Html.LabelFor(model =&gt; model.ManifestID)
      @Html.EditorFor(model =&gt; model.ManifestID)
      @Html.ValidationMessageFor(model =&gt; model.ManifestID)
  
      @Html.DropDownListFor(model =&gt; model.ProjectID, Model.Projects)
      ....
  
      &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public class ProjectManifestsController : Controller
  {
      [HttpGet]
      public ActionResult Index()
      {
          Models.ProjectManifestModel model = new Models.ProjectManifestModel();
          DatabaseHelper.ConnectionString = ConfigurationManager.ConnectionStrings["MTConnectionString"].ConnectionString;
          ProjectRepository pr = new ProjectRepository();
          List&lt;SelectListItem&gt; projects = new List&lt;SelectListItem&gt;();
          foreach (Project p in pr.GetProjects())
          {
              projects.Add(new SelectListItem { Value = p.ProjectID.ToString(), Text = p.ProjectName });
          }
          model.Projects = projects;
          ViewBag.Projects = projects;
          return View();
      }
  }
  </code></pre>
  
  <p>ViewModel</p>
  
  <pre><code>public class ProjectManifestModel
  {
      [Key]
      public int ManifestID { get; set; }
      public int ProjectID { get; set; }
      ....
      public HttpPostedFileBase Attachment { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Projects { get; set; }
  
  }
  </code></pre>
  
  <p>The state of this code produces a different error, my apologies:</p>
  
  <blockquote>
    <p>Compiler Error Message: CS1503: Argument 3: cannot convert from 'System.Collections.Generic.IEnumerable&lt;Poscarp.TabLibrary.Project>' to 'System.Collections.Generic.IEnumerable&lt;System.Web.Mvc.SelectListItem>'</p>
    
    <p>Source Error:</p>
    
    <p>Line 29: @Html.DropDownListFor(model => model.ProjectID, Model.Projects)</p>
  </blockquote>