title: ASP MVC Razor foreach inside Javascript block
body: <p>I have a Partial View that returns a Javascript function call after I submit an Ajax form.  It takes a list of addresses and call Javascript functions to geocode and place markers on a Google Map.  When I compile the following code, I get "Conditional compilation is turned off" error around var in the ForEach line.</p>
  
  <pre><code>@model IEnumerable&lt;TestStore.Models.Address&gt;
  
  @if (Model.Count() &gt; 0)
  {
  &lt;script type="text/javascript"&gt;
      deleteMarkers();
  
      @foreach(var item in Model)
      {
          codeAddress('@item.GetAddress');
      }  
  &lt;/script&gt;
  }
  </code></pre>
  
  <p>I fiddle around with the code and the following does work without compile errors:</p>
  
  <pre><code>@if (Model.Count() &gt; 0)
  {
  &lt;script type="text/javascript"&gt;
      deleteMarkers();
  &lt;/script&gt;
  
      foreach (var item in Model)
      {
          &lt;script type="text/javascript"&gt;
              codeAddress('@item.GetAddress');
          &lt;/script&gt;
      }
  }
  </code></pre>
  
  <p>For sake of discussion, if I have longer logic that make a lot of Javascript function calls inside loops, I would much prefer to surround everything inside 1 script block. I searched around Stack Overflow and it seem that Razor syntax could go inside a script block but I don't know how that look like in my example.</p>