title: Razor MVC Populating Javascript array with Model Array
body: <p>I'm trying to load a JavaScript array with an array from my model. Its seems to me that this should be possible.</p>
  
  <p>Neither of the below ways work.</p>
  
  <p>Cannot create a JavaScript loop and increment through Model Array with JavaScript variable</p>
  
  <pre><code>for(var j=0; j&lt;255; j++)
  {
      jsArray = (@(Model.data[j])));
  }
  </code></pre>
  
  <p>Cannot create a Razor loop, JavaScript is out of scope</p>
  
  <pre><code>@foreach(var d in Model.data)
  {
      jsArray = d;
  }
  </code></pre>
  
  <p>I can get it to work with</p>
  
  <pre><code>var jsdata = @Html.Raw(Json.Encode(Model.data)); 
  </code></pre>
  
  <p>But I don't know why I should have to use JSON. </p>
  
  <p>Also while at the moment I'm restricting this to 255 bytes. In the future it could run into many MBs.</p>