Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can use razor Comment on JavaScript block/

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.

like image 226
Omid Shariati Avatar asked Apr 12 '13 11:04

Omid Shariati


People also ask

Can you use Razor in JavaScript?

Razor is a . NET assembly and doesn't run on JavaScript or in a browser. It's meant to be executed server-side.

How can you comment using Razor?

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, "<! -- -->".

How do you comment out Razor code?

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.

How do you add comments in razor view?

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 *@.


2 Answers

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!!!

like image 140
Omid Shariati Avatar answered Oct 03 '22 06:10

Omid Shariati


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>
like image 22
Murali Murugesan Avatar answered Oct 03 '22 06:10

Murali Murugesan