title: ASP.NET CORE MVC SelectList from a collection of Objects
body: <p>So I have these 2 classes representing tables in a local db using ef core migration in an asp.net core app</p>
  
  <pre><code>public class Teacher
  {
      public int ID { get; set; }
  
      public string FistName { get; set; }
  
      public string LastName { get; set; }
  
      public Class Class { get; set; }
  
      public string FullName
      {
          get
          {
              return $"{LastName} {FistName}";
          }
      }
  
      public override string ToString()
      {
          return FullName;
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class Class
  {
      public int ID { get; set; }
  
      public int TeacherID { get; set; }
  
      public string Name { get; set; }
  
  
      public Teacher Teacher { get; set; }
  
      public ICollection&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  
  <p>I want to add in my classes controller the functionality to create a new instance of the "Class" class by typing out a name in a textbox and selecting a teacher from a dropdownlist.</p>
  
  <p>Here are my create http get &amp; post methods</p>
  
  <pre><code>    public IActionResult Create()
      {
          var teachers = from s in _context.Teachers
                             where s.Class == null
                             select s;
  
          ViewData["Teachers"] = new SelectList(teachers.ToList());
          return View();
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create(Class @class)
      {
          if (ModelState.IsValid)
          {
              @class.TeacherID = @class.Teacher.ID;
              _context.Add(@class);
              await _context.SaveChangesAsync();
              return RedirectToAction(nameof(Index));
          }
          return View("Index");
      }
  </code></pre>
  
  <p>and the form for submitting user-typed data</p>
  
  <pre><code>&lt;form asp-action="Create"&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Teacher" class="control-label"&gt;&lt;/label&gt;
              &lt;select asp-for="Teacher" class ="form-control" asp-items="ViewBag.Teachers"&gt;&lt;/select&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Name" class="control-label"&gt;&lt;/label&gt;
              &lt;input asp-for="Name" class="form-control" /&gt;
              &lt;span asp-validation-for="Name" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>I can see the right data in the dropdown list but for some reason inside the POST method @class.Teacher is null. What am I doing wrong?</p>