title: Create drop down list asp.net core
body: <p>I have two view models:</p>
  
  <pre><code>public class PersonViewModel
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public DateTime Dob { get; set; }
      public string Email { get; set; }
      public ICollection&lt;PetViewModel&gt; Pets { get; set; }
  }
  
  public class PetViewModel
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int PersonId { get; set; }
      public PersonViewModel Person { get; set; }
  }
  </code></pre>
  
  <p>When creating/editing a pet I want to have a drop down list of all the people to choose from. This is my pets controller:</p>
  
  <pre><code>\\ Other methods omitted for brevity
  
      [HttpGet]
      public IActionResult Create()
      {
          var people = _personService.AsQueryable().ToList();
          ViewBag.PeopleList = new SelectList(people , "Id", "FirstName");
          return View();
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public IActionResult Create([Bind("Id,Name,PersonId")] PetViewModel vm)
      {
          if (!ModelState.IsValid) return View(vm);
          var pet = _petService.Create(vm.Name, vm.PersonId);
          return RedirectToAction("Details", pet);
      }
  </code></pre>
  
  <p>I am trying to do it via ViewBag and doing the following in the view as such:</p>
  
  <pre><code>@model UI.ViewModels.PetViewModel
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  &lt;form asp-action="Create"&gt;
    &lt;div class="form-horizontal"&gt;
    &lt;h4&gt;Create a Pet&lt;/h4&gt;
     &lt;div class="form-group"&gt;
          &lt;label asp-for="PersonId" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;
              @Html.DropDownListFor(model =&gt; model.PersonId, ViewBag.PeopleList, "--Select--", new { @class = "form-control"})
              &lt;span asp-validation-for="PersonId" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Upon trying to do so I get the following error: <code>Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.</code></p>
  
  <p>Any help is appreciated. Thanks!</p>