如何在javascript脚本标签中使用剃须刀for循环? [英] How can a razor for loop be used in a javascript script tag?

查看:64
本文介绍了如何在javascript脚本标签中使用剃须刀for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成的错误是条件编译已关闭".

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

来自MSDN的条件编译黑客很普遍,但失败了.

Conditional Compilation hack from MSDN is prevalent but fails.

有几个类似的问题: 在Razor中关闭了条件编译?

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

他们都指向以下答案:

/*@cc_on @*/

从MSDN上的文章中可以看到:

From the article seen here at the MSDN:

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

但是,此hack相当失败,或者我似乎在实现它时失败了.尾随@*导致.cshtml文件中的其余代码被注释掉.此外,@cc_on给出错误当前上下文中不存在cc_on".

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

这是一段要在.cshtml文件中进行测试的代码:

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>

这将导致条件编译已关闭"消息.尝试在该处插入替代方法将导致for循环中的各种其他消息(例如,"cc_on"在上下文," expected,或" expected;或" expected)中不存在).

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.

如何在javascript脚本标记中使用剃须刀for loop?

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

推荐答案

尝试用<text></text>

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

这篇关于如何在javascript脚本标签中使用剃须刀for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆