title: Rendering a JsonResult to a string in Razor
body: <p>I have a server-generated object that I need to convert to a JSON object for JavaScript to consume.  I prefer to render this JSON object directly into a JS variable when the view renders to prevent an additional HTTP request.</p>
  
  <p><strong>This is in my controller:</strong></p>
  
  <pre><code>public virtual JsonResult GetTheThings()
  {
      return Json(new
      {
          foo = "hello world",
          bar = 3,
      }, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>I can access this directly at <code>http://localhost:32243/MyController/GetTheThings</code> and I get the following rendered in my browser.
  <code>{"foo":"hello world", "bar":3}</code>. Perfect!</p>
  
  <p>So, now I basically just want to render the result of this view into a string. How do I do this? What I have below does <em>not</em> work, but hopefully it give you the idea.</p>
  
  <p><strong>This is my attempt</strong></p>
  
  <pre><code>&lt;script&gt;
    var myObj = @Html.RenderPartial(MVC.MyController.GetTheThings());
  &lt;/script&gt;
  </code></pre>
  
  <p>Note that I am also using T4 Templates.</p>
  
  <p><strong>In the end, this is what I want to be rendered in the view.</strong></p>
  
  <pre><code>&lt;script&gt;
    var myObj = {"foo":"hello world", "bar":3};
  &lt;/script&gt;
  </code></pre>