title: Is it possible to refresh a View Component in an ASP.NET Core 2.1 Razor page?
body: <p>Is it possible to refresh a View Component in an ASP.NET Core 2.1 Razor page?</p>
  <p>I discovered that this is possible in ASP.NET MVC using AJAX—see solutions at <a href="https://stackoverflow.com/questions/32618759/viewcomponent-alternative-for-ajax-refresh/32628302#32628302">Viewcomponent alternative for ajax refresh</a> and <a href="https://stackoverflow.com/questions/36024748/viewcomponents-are-not-async?noredirect=1&amp;lq=1">ViewComponents are not async</a>. But what about ASP.NET Core?</p>
  <p>One solution I found is basically to have a controller returning the View Component, and then using AJAX to do the refresh.</p>
  <p>This is example is borrowed from <a href="https://stackoverflow.com/questions/36024748/viewcomponents-are-not-async?noredirect=1&amp;lq=1">ViewComponents are not async</a></p>
  <p><strong>~/HelloWorldViewComponent.cs</strong></p>
  <pre><code>public class HelloWorldViewComponent : ViewComponent
  {
      public IViewComponentResult Invoke()
      {
          var model = new string[]
          {
              &quot;Hello&quot;, &quot;from&quot;, &quot;the&quot;, &quot;view&quot;, &quot;component.&quot;  
          };
  
          return View(&quot;Default&quot;, model);
      }     
  }
  </code></pre>
  <p><strong>~/HomeController.cs</strong> (I cannot get this part working in razor pages)</p>
  <pre><code>public class HomeController : Controller
  {
      public IActionResult GetHelloWorld()
      {
          return ViewComponent(&quot;HelloWorld&quot;);
      }
  }
  </code></pre>
  <p><strong>~/Views/Shared/Components/HelloWorld/Default.cshtml</strong></p>
  <pre><code>@model string[]
  &lt;ul&gt;
      @foreach(var item in Model)
      {
          &lt;li&gt;@item&lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  <p><strong>~/wwwroot/index.html</strong></p>
  <pre><code>&lt;body&gt;
  &lt;script src=&quot;js/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
      $.get(&quot;home/GetHelloWorld&quot;, function(data) {
          $(&quot;body&quot;).html(data);
      });
  &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  <p>My problem is that I cannot get a controller working together with Razor Pages, I cannot figure out if that's even possible.</p>