title: ASP.NET MVC 4 - Razor Within Javascript Function Within @Section Causes Scope Error
body: <p>The problem is that placing a C# variable within a JS function within a @Section produces incorrect javascript (in MVC 4 only).</p>
  
  <p>In a JS function in MVC 3, everything executes as expected.</p>
  
  <pre><code>@section test {
  
  &lt;script type="text/javascript"&gt;
      $(function () {
          alert(@DateTime.Now);
      });
  &lt;/script&gt;
  } 
  </code></pre>
  
  <p>Take this exact same code and place it in an MVC4 app and you will get malformed JS.</p>
  
  <p>The HTML on the page will actually render the following:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
          alert(12/27/2011 11:04:04 AM);
  </code></pre>
  
  <p>and the html will emit</p>
  
  <blockquote>
    <p>);    }</p>
  </blockquote>
  
  <p>Note the closing script tag is not produced.</p>
  
  <p>It appears the closing curly brace in the JS function is confused the be the closing curly brace in Razor.</p>
  
  <p>Note that I am also declaring a RenderSection("test",false") in my _Layout.cshtml file.</p>