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

c# - How can a razor for loop be used in a javascript script tag? - Stack Overflow

programmeradmin0浏览0评论

The error generated is "Conditional Compilation is turned off".

Conditional Compilation hack from MSDN is prevalent but fails.

There are several questions like this one: Conditional Compilation is turned off in Razor?

They all point to the answer of:

/*@cc_on @*/

From the article seen here at the MSDN:

(v=vs.90).aspx

However, this hack is pretty fail or I seem to fail at implementing it. The trailing @* causes the remaining code in the .cshtml file to become commented out. Moreover, @cc_on gives an error "cc_on does not exist in the current context".

Here is a piece of code to test in a .cshtml file:

<script type="text/javascript">
 @for(int i = 0; i < 5; i++)
 {
    document.write(@i);
 }
</script>

Which will cause the "Conditional Compilation is turned off" message. Attempting to insert the workaround in there will cause various other messages such as "cc_on" does not exist in the context", "expected ,", or "expected ;", or "expected )" from the for loop.

How can a razor for loop be used in a javascript script tag?

The error generated is "Conditional Compilation is turned off".

Conditional Compilation hack from MSDN is prevalent but fails.

There are several questions like this one: Conditional Compilation is turned off in Razor?

They all point to the answer of:

/*@cc_on @*/

From the article seen here at the MSDN:

http://msdn.microsoft.com/en-us/library/5y5529x3(v=vs.90).aspx

However, this hack is pretty fail or I seem to fail at implementing it. The trailing @* causes the remaining code in the .cshtml file to become commented out. Moreover, @cc_on gives an error "cc_on does not exist in the current context".

Here is a piece of code to test in a .cshtml file:

<script type="text/javascript">
 @for(int i = 0; i < 5; i++)
 {
    document.write(@i);
 }
</script>

Which will cause the "Conditional Compilation is turned off" message. Attempting to insert the workaround in there will cause various other messages such as "cc_on" does not exist in the context", "expected ,", or "expected ;", or "expected )" from the for loop.

How can a razor for loop be used in a javascript script tag?

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Jul 2, 2012 at 23:46 Travis JTravis J 82.3k42 gold badges211 silver badges279 bronze badges 2
  • This looks like a duplicate of stackoverflow.com/questions/4599169/… – HatSoft Commented Jul 3, 2012 at 0:02
  • 1 @HatSoft - For the most part it is, although they do not mention the conditional compilation warning which was the aim of this question. Using @: and <text> work here to generate javascript from razor but the warnings from "conditional compilation" still remain. – Travis J Commented Jul 3, 2012 at 18:33
Add a comment  | 

2 Answers 2

Reset to default 14

Try surrounding your js with <text></text>

<script type="text/javascript">
 @for(int i = 0; i < 5; i++)
 {
    <text>var that = this;</text>
 }
</script>

I need to loop over a model attribute and just serialized it.

<script type="text/javascript">
    var items = @Html.Raw(Json.Serialize(Model.items))

    items.forEach(function (item) {
        console.log(item)
    })
</script>
发布评论

评论列表(0)

  1. 暂无评论