title: Dropdownlist in viewModel create view
body: <p>I have a "Leads" table which has a one to many relationship to "Employees" table. I also have created a leadViewModel class because I need some additional info for the Lead (Like addresses and phones etc.)</p>
  
  <p>Here is the Lead class:</p>
  
  <pre><code>public partial class Leads
  {
      [Key]
      public int LeadID { get; set; }
  
      public string FirstName { get; set; }
      public string LastName { get; set; }
      [NotMapped]
      public string FullName { get { return FirstName + " " + LastName; } }
  
      public int EmployeeID { get; set; }
  
      public virtual Employees Employees { get; set; }
  }
  </code></pre>
  
  <p>Employee Class:</p>
  
  <pre><code>public partial class Employees
  {
      public Employees()
      {
          this.Leads = new HashSet&lt;Leads&gt;();
      }
  
      [Key]
      public int EmployeeID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      [NotMapped]
      public string FullName { get { return FirstName + " " + LastName; } }
  
      public virtual ICollection&lt;Leads&gt; Leads { get; set; }
  }
  </code></pre>
  
  <p>leadViewModel:</p>
  
  <pre><code>public class LeadViewModel
  {
      public Leads _leads { get; set; }
      public Addresses _addresses { get; set; }
      public Phones _phones { get; set; }
      public Emails _emails { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; EmployeesSelectListItem { get; set; }
  }
  </code></pre>
  
  <p>Now I want to have a dropdown list in the "Create" view. I am not sure what to do. Here is what I did and got errors.</p>
  
  <p>LeadsController Create Get:</p>
  
  <pre><code>    // GET: Leads/Create
      public ActionResult Create()
      {
          ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName");
  
          return View();
      }
  </code></pre>
  
  <p>LeadsController Create Post:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]        
      public ActionResult Create([Bind(Include = "_leads,_addresses,_phones,_emails,EmployeesSelectListItem")] LeadViewModel leadVM)
      {            
          if (ModelState.IsValid)
          {
              using (TransactionScope leadScope = new TransactionScope())
              {
                  db.Leads.Add(leadVM._leads);
                  db.SaveChanges();
  
                  leadScope.Complete();
  
                  return RedirectToAction("Index");
              }
          }
  
          ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName", leadVM._leads.EmployeeID);
  
          return View(leadVM);
      }
  </code></pre>
  
  <p>and finally the create view:</p>
  
  <pre><code>@model CL_AHR_CRM.ViewModels.LeadViewModel
  
  ...
  
     &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model._leads.EmployeeID, "EmployeeID", htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.DropDownListFor(x =&gt; x._leads.EmployeeID, new SelectList(Model.EmployeesSelectListItem, "EmployeeID", "FirstName"), htmlAttributes: new { @class = "form-control" })
              @Html.ValidationMessageFor(model =&gt; model._leads.EmployeeID, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I repeat my question: How can I use dropdownliast in a view when using a viewModel like this because obviously what I have done is not the way and is not working. I get the error: "Object reference not set to an instance of an object." on the Html.DropdownlistFor(...) in create view.</p>