Razor bug: Javascript inside of a @sectionhttp://forums.asp.net/t/1773194.aspx/1?Razor+bug+Javascript+inside+of+a+sectionFri, 27 Apr 2012 23:10:15 -040017731944848102http://forums.asp.net/p/1773194/4848102.aspx/1?Razor+bug+Javascript+inside+of+a+sectionRazor bug: Javascript inside of a @section <p>Consider the following block of code in a Razor view:</p> <pre class="prettyprint">@section HeaderContent { &lt;script type=&quot;text/javascript&quot;&gt; $(function () { for (var x = 0; x &lt;= 5; x&#43;&#43;) { // blah } $(&quot;.rate div&quot;).click(function () { // blah }); }); &lt;/script&gt; }</pre> <p>The &lt;= causes the last closing brace inside of the script block to close the section. Change it to a just =, it's fine. Change it to &lt;, and the block doesn't close at all.</p> 2012-02-23T21:29:31-05:004848139http://forums.asp.net/p/1773194/4848139.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>I copied your code in a page of one of my projects...and it works! try to insert it into a fresh new project,...you will see that it works. Probably there is some other error in the page that causes this behaviour</p> 2012-02-23T22:05:30-05:004848148http://forums.asp.net/p/1773194/4848148.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>That may be true (I didn't supply the layout page, obviously), but that doesn't make it less broken.</p> 2012-02-23T22:20:30-05:004848308http://forums.asp.net/p/1773194/4848308.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>It keeps getting more weird. If I add another line at the end of the above function, like this:</p> <pre class="prettyprint">MyNamespace.MyFunction(i, id[0], id[1]);</pre> <p>I actually need another closing brace for the section, which ends up rendering a brace in the markup, which is also not desirable. Looks like I'm not the only one to see this:</p> <p><a href="http://stackoverflow.com/questions/9366169/mvc4-razor-confused-about-braces" target="_blank">http://stackoverflow.com/questions/9366169/mvc4-razor-confused-about-braces</a></p> 2012-02-24T02:47:26-05:004848933http://forums.asp.net/p/1773194/4848933.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>please prepare a project with just 2 pages the Layout page and the page using the section...with no other code ...just the one needed to cause the issue to appear and send it to me. You can use the contact form of my blog.</p> 2012-02-24T09:25:29-05:004849385http://forums.asp.net/p/1773194/4849385.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>Are you with Microsoft?</p> 2012-02-24T13:30:12-05:004849422http://forums.asp.net/p/1773194/4849422.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Jeff</h4> Are you with Microsoft?</blockquote> <p></p> <p>No</p> <p>However, this is the minimum to do before submitting a bug report to Microsoft, I read the discussion on StackOverflow...but strangely this proble never happened to me. So I &quot;suspect&quot; it just takes place if there is some &quot;pervious small error&quot;. That is there is something that Razor &quot;tolerate&quot; but that eventually may cause problems in some circumstances.</p> 2012-02-24T13:50:52-05:004849433http://forums.asp.net/p/1773194/4849433.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>I worked for Microsoft, and I have a repro solution. I asked a friend on the MVC team where to submit to, and he suggested posting it here and on SO.</p> 2012-02-24T13:56:12-05:004849473http://forums.asp.net/p/1773194/4849473.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>Does you repro solution....is very simply and just contains the section definition in the Layoiut page and the script causing the problem?&nbsp;</p> <p>Or does your solution contains also other stuffs?</p> 2012-02-24T14:11:43-05:004849974http://forums.asp.net/p/1773194/4849974.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>Similar error occurred when I converted an MVC3 project to MVC4 Developer preview.</p> <p>It was also a javascript block inside an @section. The opening brace of the @section got closed by a closing brace inside the javascript block inside the @section.</p> <p>Must be a bug in Razor2 since it was working fine in MVC3.</p> <p>The work around I implemented was to move the script from the @section to the main section.</p> 2012-02-24T19:28:39-05:004850988http://forums.asp.net/p/1773194/4850988.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Jeff</h4> <a title="Jeff" href="/members/Jeff.aspx">Jeff</a></blockquote> <p></p> <p>Yes, That is a bug.</p> 2012-02-26T00:36:21-05:004862279http://forums.asp.net/p/1773194/4862279.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Jeff</h4> <p></p> <p>Consider the following block of code in a Razor view:</p> <pre class="prettyprint">@section HeaderContent { &lt;script type=&quot;text/javascript&quot;&gt; $(function () { for (var x = 0; x &lt;= 5; x&#43;&#43;) { // blah } $(&quot;.rate div&quot;).click(function () { // blah }); }); &lt;/script&gt; }</pre> <p>The &lt;= causes the last closing brace inside of the script block to close the section. Change it to a just =, it's fine. Change it to &lt;, and the block doesn't close at all.</p> <p></blockquote></p> <p>An ugly workaround of this may be to add &gt; sign(note I added <strong>2 &gt; 1 &amp;&amp;</strong>), like,</p> <pre class="prettyprint">@section HeaderContent { &lt;script type="text/javascript"&gt; &#36;(function () { for (var x = 0; x &lt;= 5 &amp;&amp; 2 &gt; 1 ; x++) { // blah } &#36;(".rate div").click(function () { // blah }); }); &lt;/script&gt; }</pre> <p>or rewrite the &lt; sign as &gt; sign, like,</p> <pre class="prettyprint">@section HeaderContent { &lt;script type="text/javascript"&gt; &#36;(function () { for (var x = 0; 5 &gt;= x; x++) { // blah } &#36;(".rate div").click(function () { // blah }); }); &lt;/script&gt;</pre> <p></p> </blockquote> 2012-03-03T18:05:03-05:004955527http://forums.asp.net/p/1773194/4955527.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section <p>I found a less annoying work around when I came across the same issue. Leave the script inside the section, just wrap it in a razor explicti &lt;text&gt; block like this:</p> <p></p> <pre class="prettyprint">@section HeaderContent { @if(true){ &lt;text&gt; &lt;script type=&quot;text/javascript&quot;&gt; $(function () { for (var x = 0; x &lt;= 5; x&#43;&#43;) { // blah } $(&quot;.rate div&quot;).click(function () { // blah }); }); &lt;/script&gt; &lt;/text&gt; } }</pre> <p>It is still ugly, but it doesn't require you to move the script out of the section, or alter the javascript itself.&nbsp;<br> <br> </p> 2012-04-27T20:56:48-04:004955609http://forums.asp.net/p/1773194/4955609.aspx/1?Re+Razor+bug+Javascript+inside+of+a+sectionRe: Razor bug: Javascript inside of a @section They fixed it in the repo on CodePlex already after I filed a bug there. If you build latest, it'll work as expected. 2012-04-27T23:10:15-04:00