title: Select tag helper from database ASP.NET Core 3.1
body: <p>Ok, I'm trying to do a proper dropdown in Core 3.1. In this example <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1#the-select-tag-helper" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1#the-select-tag-helper</a> 
  Model has a new list with hardcoded values</p>
  
  <pre><code>public string Country { get; set; }
  
  public List&lt;SelectListItem&gt; Countries { get; } = new List&lt;SelectListItem&gt;
  {
     new SelectListItem { Value = "MX", Text = "Mexico" },
     new SelectListItem { Value = "CA", Text = "Canada" },
     new SelectListItem { Value = "US", Text = "USA"  },
  };
  </code></pre>
  
  <p>I looked for examples where the list is coming from the database but they are very inconsistent. The only way I was able to do the dropdown list is with the ViewBag which is not advised. </p>
  
  <p>I have two models. 1.</p>
  
  <pre><code> public partial class Glossary
      {
          public int UniqueId { get; set; }
          public int Category { get; set; }
          public string DisplayText { get; set; }
      }
  </code></pre>
  
  <ol start="2">
  <li>which is my view model</li>
  </ol>
  
  <pre><code> public partial class AdminUser
      {
          [Key]
          public int Id { get; set; }
          public string UserName { get; set; }
          public string UserLocation { get; set; }
          public string UserStatus { get; set; }
          //public IEnumerable&lt;Glossary&gt; Glossary { get; set; } //I used this for ViewBag 
          public List&lt;SelectListItem&gt; UserLocations { get; } = new List&lt;SelectListItem&gt;
          {
             according to the example my query should go here
          };      
      }
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>        public IActionResult Create()
          {
             // This is the ViewBag that worked with HTML helpers, but I'm trying to use tag-helpers.
              /*IEnumerable&lt;SelectListItem&gt; LocationsList = _context.Glossary.Where(x =&gt; x.Category == 1).Select(x =&gt; new SelectListItem
              {
                  Value = x.UniqueId.ToString(),
                  Text = x.DisplayText
              });
              ViewBag.LocationsList = LocationsList;
             */
              return View();
          }
  </code></pre>
  
  <p>All examples that found were hardcoded lists and nothing with getting it from the database. What is the proper way to get the data from the Glossary table through the view model with ViewBag? Any pointers are appreciated. </p>
  
  <p>ALSO:
  When using this example: <a href="https://stackoverflow.com/questions/34624034/select-tag-helper-in-asp-net-core-mvc">Select Tag Helper in ASP.NET Core MVC</a>
  When I used </p>
  
  <pre><code>public SelectList Employees { set; get; }
  </code></pre>
  
  <p>I got error: InvalidOperationException: The entity type 'SelectListGroup' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.</p>
  
  <p>Both of my tables have PK and adding [Key] to Glossary model didn't fix it. </p>