title: Use SQL Query for Dropdown - ASP.NET Core 2.0; RazorPages; VS 2017; Entity Framework Core
body: <p>I currently have an ASP.NET Core 2.0 Web Application (built in VS 2017 from the default web application template).  I have a RazorPage with a dropdown built in that I would like to populate via a SQL Query.  I've used Entity Framework Core to build a model of my database as follows:</p>
  
  <pre><code>    public partial class INVOPEContext : DbContext
      {
          public virtual DbSet&lt;PmeFundData&gt; PmeFundData { get; set; }
  
              modelBuilder.HasAnnotation"Relational:DefaultSchema", "Server\\User");
  
              modelBuilder.Entity&lt;PmeFundData&gt;(entity =&gt;
              {
                  entity.ToTable("PME_FUND_DATA", "dbo");
                  entity.Property(e =&gt; e.Firm).HasMaxLength(255);
              });
      }
  
  
      public partial class PmeFundData
      {
          public string Firm { get; set; }
      }
  </code></pre>
  
  <p>I've updated the RazorPage PageModel (pmepe.cshtml.cs) to include the DBContext and Query:</p>
  
  <pre><code>    public class pmepeModel : PageModel
      {
          private readonly INVOPEContext _db;
          public pmepeModel(INVOPEContext db)
          {
              _db = db;
          }
  
          public IActionResult dropdowns()
          {
              List&lt;PmeFundData&gt; firmList = new List&lt;PmeFundData&gt;();
  
              firmList = (from Firm in _db.PmeFundData
                          select Firm).Distinct().ToList();
  
              firmList.Insert(0, new PmeFundData {Firm = "All Firms" });
  
              ViewBag.ListofFirms = firmList;
  
              return View();
          }
      }
  </code></pre>
  
  <p>Finally, the view with the dropdown (pmepe.cshtml) is as follows:</p>
  
  <pre><code>    @page
      @model pmepeModel
      @{
          ViewData["Title"] = "pmepe";
      }
  
      &lt;select asp-for="dropdowns"
          id="firm" 
          class="dropdown" 
          asp-items= "@(new SelectList(ViewBag.ListofFirms, "Firm"))"&gt;
      &lt;/select&gt;
  </code></pre>
  
  <p>I am getting errors that neither ViewBag nor View exist in the current context in the PageModel (no errors in the view - Intellisense picks it up).  Every example I've found on the web is for MVC rather than RazorPages.  The solution commonly provided for MVC is imbedding the query in a Controller and adjusting the MVC version in the web.config file.  But the RazorPages template doesn't have Controllers and I can't find a web.config file - so I haven't been able to get it to work in my app.  Any help you can provide would be most appreciated.</p>