title: What's the "right way" to access data in a .Net Core partial view in a layout?
body: <p>I have a partial view called _SponsorPartial.cshtml being called in my _Layout.cshtml so that it's on every page:</p>
  
  <pre><code>    &lt;footer&gt;
          &lt;div class="visible-xs-inline-block"&gt;
              @await Html.PartialAsync("_SponsorPartial")
          &lt;/div&gt;
      &lt;/footer&gt;
  </code></pre>
  
  <p>I'm using .Net core so I have my dbContext ready to be injected where I need it:</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddEntityFramework()
              .AddSqlServer()
              .AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
         [...]
      }
  </code></pre>
  
  <p>_SponsorPartial is a Bootstrap carousel of images (database driven) based on a value that is different on different pages.  I could very easily build a function to get my list based on a parameter:</p>
  
  <pre><code> public static List&lt;SomeObject&gt; GetObjects(int myParameter)
  </code></pre>
  
  <p>My initial thoughts were to access @ViewData["myParameter"] in the partial page, then have the partial page get the appropriate information from the function.  </p>
  
  <p>The issue is...How do I create a function in some class someplace that has access to the context objects?  Specifically ApplicationDbContext.  And how do I call it?!  In a controller I know I can inject ApplicationDbContext and pass it around...but this is a partial page...in a layout.</p>
  
  <p>And the big question is of course...what is the "right" way to do this in an .Net Core (MVC) environment?  What is the right way to access database driven information from a partial view buried in a layout shown by almost every controller on the site.</p>