最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - How can use razor Comment on JavaScript block - Stack Overflow

programmeradmin4浏览0评论

When I create a razor ment 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 ment like below code ( razor ment and a javascript ment 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 ment 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.

When I create a razor ment 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 ment like below code ( razor ment and a javascript ment 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 ment 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.

Share Improve this question edited Apr 15, 2013 at 4:29 Omid Shariati asked Apr 12, 2013 at 11:38 Omid ShariatiOmid Shariati 1,9165 gold badges22 silver badges45 bronze badges 10
  • Why not just use JavaScript ment syntax? Also why are you using inline JavaScript? – James South Commented Apr 12, 2013 at 11:40
  • Can you paste some code? Comments should not affect this. – Darren Commented Apr 12, 2013 at 11:41
  • Why do you want ment? If needed you can do it with conditional statement like if :) – Murali Murugesan Commented Apr 12, 2013 at 11:44
  • 1 @OmidShariati Then stick them just before the script block. Either way you shouldn't have JavaScript nested in Razor script. It's a maintenance nightmare. Move your script to a separate file where it belongs and your problem will go away. – James South Commented Apr 12, 2013 at 11:52
  • 1 @JamesSouth is absolutely correct. – Joel Cochran Commented Apr 19, 2013 at 13:07
 |  Show 5 more ments

2 Answers 2

Reset to default 5

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>
发布评论

评论列表(0)

  1. 暂无评论