Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax Error when writing @Html.Raw(Html.Debug) in ASP.NET MVC
    primarykey
    data
    text
    <p>i'm trying to combine C# code with javascript in MVC to enable "debug" only when i'm using the page in debug mode.</p> <p>Here i get an error:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { Control.init( "@(ViewBag.Control)", @Html.Raw(Html.IsDebug() ? "true" : "false") ); }); &lt;/script&gt; </code></pre> <p>In the line: @Html.Raw(...) i get "Syntax error"</p> <p>Full code:</p> <pre><code>@{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; @Styles.Render("~/Content/css/normalize") @Styles.Render("~/Content/css/bootstrap") @Scripts.Render("~/bundles/modernizr") @RenderSection("head", false) &lt;/head&gt; &lt;body&gt; @RenderBody() @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/js/control") &lt;script type="text/javascript"&gt; $(document).ready(function () { Control.init( "@(ViewBag.Control)", @Html.Raw(Html.IsDebug() ? "true" : "false") ); }); &lt;/script&gt; @RenderSection("scripts", false) &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Is debug Method:</p> <pre><code>using System.Web.Mvc; namespace System.Web.Mvc { public static class GUIHelper { public static bool IsDebug(this HtmlHelper htmlHelper) { #if DEBUG return true; #else return false; #endif } } } </code></pre> <p>When i do:</p> <pre><code>"@(Html.IsDebug() ? "true" : "false")" </code></pre> <p>instead of:</p> <pre><code>@(Html.IsDebug() ? "true" : "false") </code></pre> <p>works, but...:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { Control.init( "Control1", "true" // instead of true ); }); &lt;/script&gt; </code></pre> <hr> <p><strong>* Update: 2013-11-27 *</strong></p> <p>Solution 1:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { Control.init( "@(ViewBag.Control)", "@(Html.IsDebug() ? "true" : "false")" == "true" ); }); &lt;/script&gt; </code></pre> <p>Solution 1:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { @Html.Raw("Control.init("+ViewBag.Control+","+(Html.IsDebug() ? "true" : "false")+")") }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload