When I create a razor comment block into a script block in cshtml file, the javascript intelisense gives me error. How we can solve this? hint: I use VisualStudio 2012 and Resharper 7.1.2
I know that we can use nested comment like below code ( razor comment and a javascript comment outer of that)
<script type="text/javascript">
//@*
var something = "bla bla";
//*@
var other = "something else";
</script>
it must work properly but there are two problem 1- intelisense gets me error yet 2- Since razor ignore spaces, the rest of my codes after closing comment symbol ( *@ )
Edit1:
other things that I tried was:
/*@*
var something = "bla bla";
*@*/
var other = "something else";
and
/*@*
var something = "bla bla";
*@*/
var other = "something else";
. but they have some problem too.
Razor is a . NET assembly and doesn't run on JavaScript or in a browser. It's meant to be executed server-side.
Comments Razor uses the syntax "@* .. *@" for the comment block but in a C# code block we can also use "/* */" or "//". HTML comments are the same, "<! -- -->".
In visual studio, select some code/markup in your razor view and press Ctrl+K, Ctrl+C, and it'll comment the selection as described above.
To comment in the code block of Razor view, we use the same syntax as we use in C#. Like for single line // and for multiline /* and */. To comment, HTML along with other code, we need to use razor comment block that starts with @* and ends with *@.
I find my answer that is:
<script type="text/javascript">
/*@*
var something = "bla bla";
*@//*/
var other = "something else";
</script>
It will be rendered as
/*//*/
var other = "something else";
visual studio intelisense and resharper works properly!!!
If I understand your problem, you want to define a javascript variable based on some condition.
If that is true, you can try the below
<script type="text/javascript">
@if (Model.UserCanSee)
{
<text>
var uiEnabled=true;
</text>
}
else{
<text>
var uiDisabled=true;
</text>
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With