title: PartialView To Html String
body: <p>I have a workflow that uses signalR to update a page as data comes in from the service.  Currently the flow looks like:</p>
  
  <pre><code>Service -&gt; SignalR Hub           Controller
                         v      ^              v
                           View                   Partial
  </code></pre>
  
  <p>The top line is server side, bottom is client.  I use signalR to reach my jquery function, that uses AJAX to get the partial view from the controller and update the page.</p>
  
  <p>It seems like I could shorten this process to :</p>
  
  <pre><code>Service -&gt; Controller -&gt; SignalR Hub
                                       v
                                          View
  </code></pre>
  
  <p>Where the controller would send the string representation of the partial view to the SignalR Hub.</p>
  
  <p>My problem is trying to get the string representation of the partial view.  I've looked around and found this code:</p>
  
  <pre><code>protected string RenderPartialViewToString(string viewName, object model)
      {
          if (string.IsNullOrEmpty(viewName))
              viewName = ControllerContext.RouteData.GetRequiredString("action");
  
          ViewData.Model = model;
  
          using (StringWriter sw = new StringWriter())
          {
              ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
              ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
              viewResult.View.Render(viewContext, sw);
  
              return sw.GetStringBuilder().ToString();
          }
      }
  </code></pre>
  
  <p>But this requires the ControllerContext, which for me is always null as I am not calling this method from the client.  I have not found a way to render the partial view without the ControllerContext.  Is it possible to do this?</p>