title: Accessing ViewData in jQuery not working
body: <p>I am trying to pass an IList back to my View where it can be accessible in a jQuery .each(), but I'm not having any success. </p>
  
  <p>Example class:</p>
  
  <pre><code>public class MyClass
  {
      private string _Name = "";
      public int Id = null;
  
      public MyClass ( string Name, int Id = null )
      {
         ... Do something ...
      }
  
      public string Name 
      { 
          get
          {
              return _Name;
          }
          set
          {
              _Name = value;
          }
      }
  }
  </code></pre>
  
  <p>In Controller:</p>
  
  <pre><code>IList&lt;MyClass&gt; myClass = null;
  myClass = GetNames();
  
  ViewDataDictionary viewData = new ViewDataDictionary(null);
  viewData.Add("myNames", Json(myClass));
  
  // The string writer where to render the HTML code of the view
  StringWriter stringWriter = new StringWriter();
  
  // Render the Index view in a HTML string
  ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext,
      "GeneratePDF", null);
  
  ViewContext viewContext = new ViewContext(
      ControllerContext,
      viewResult.View,
      viewData,
      new TempDataDictionary(),
      stringWriter
      );
  viewResult.View.Render(viewContext, stringWriter);
  
  ... Omitted for brevity ...
  </code></pre>
  
  <p>In the View I would like to use jQuery to iterate through ViewData["myNames"]:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      _myNames = '@ViewData["myNames"]';
  
      $.each(_myNames, function (i, item) {
          ...
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>The above doesn't work and unfortunately I'm not sure how to debug it because the view is being called by EvoPDF PDF Generator.</p>
  
  <p>I've also tried this in my View:</p>
  
  <pre><code>&lt;input type="hidden" id="myNames" value="@ViewData["myNames"]" /&gt;
  
  &lt;script type="text/javascript"&gt;
  
      _myNames = $('#myNames').val();
  
      $.each(_myNames, function (i, item) {
  
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Tracing through the controller code (where EvoPDF renders the page) I can see that the hidden field is containing the Type Name in the value attribute - not the data.</p>
  
  <p>I've used ViewData many times without problem, but this is a bit different usage than I'm familiar with. Can anyone tell me how to pass an IList/List/IENumerable using ViewData so that I can iterate through it with jQuery .each()?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As an alternative, I could make a jQuery call from within my razor, but I'm not sure how to pass the variable to jQuery:</p>
  
  <pre><code>@foreach (var myName in (List&lt;MyClass&gt;)ViewData["myNames"])
  {
      &lt;tr&gt;
      &lt;td class="text-center"&gt;
          &lt;script type="text/javascript"&gt;
              FormatName('@myName.Name', 1);
          &lt;/script&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p>But <code>myName.Name</code> doesn't seem to being passed to the FormatName() function. I know for a fact that myName.Name contains a valid value because it renders fine without the jQuery. How do I pass that var to my jQuery function?</p>