Razor 语法和 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4045308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor Syntax and Javascript
提问by Kieron
As a test I'm converting a proof-of-concept app we've written from Web Forms to Razor, simply so we can evaluate it.
作为测试,我正在将我们从 Web 表单编写的概念验证应用程序转换为 Razor,以便我们对其进行评估。
I've run into one problem so far that's making my head hurt..generating client-side Javascript...
到目前为止,我遇到了一个让我头疼的问题……生成客户端 Javascript ……
Web-Forms
网页表格
<script type="text/javascript">
var jqGridIdList = "<%: Url.Action ("getidlist", "office", new { area = "reports" }) %>";
var availableIds = [];
<% for (var i = 0; i < Model.Data.Count (); i++) { %>
availableIds.push({ value : "<%: Model.Data.ElementAt (i).Text %>", label : "<%: Model.Data.ElementAt (i).Text %>" });
<% } %>
</script>
Razor Syntax
剃刀语法
<script type="text/javascript">
var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })";
var availableIds = [];
@for(var i = 0; i < Model.Data.Count (); i++) {
availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" });
}
</script>
The compiler gives me the following error on the 'availableIds.push' line:
编译器在“availableIds.push”行上给了我以下错误:
Compiler Error Message: CS1525: Invalid expression term '{'
编译器错误消息:CS1525:无效的表达式术语“{”
It's obviously trying to compile it as C#...but how do I stop it?
它显然试图将其编译为 C#...但我该如何阻止它?
Thanks,
Kieron
谢谢,
基隆
回答by Buildstarted
You need to wrap it in the pseudo element <text>
. This will switch the parser back to html mode and it will then parse the javascript as part of the html and not c#. The reason it happens is the @for()
is a c# block and anything treated within is also considered c# until it's escaped by an html tag. Since you probably don't want an html tag razor provides the <text>
tag to switch modes.
您需要将其包装在伪元素中<text>
。这会将解析器切换回 html 模式,然后它将 javascript 解析为 html 而不是 c# 的一部分。它发生的原因是@for()
is ac# 块并且在其中处理的任何内容也被视为 c# 直到它被 html 标记转义。由于您可能不希望 html 标签 razor 提供<text>
用于切换模式的标签。
If you notice the difference in your asp.net webforms you end the <% for
line with a %>
which takes it out of c# mode. If you download the razor highlighter extension for visual studio 2010 it will help you see when code is treated as code and html is treated as html.
如果你注意到你的 asp.net webforms 的不同,你可以<% for
用 a结束该行,%>
这将它从 c# 模式中移除。如果您下载 Visual Studio 2010 的剃刀荧光笔扩展,它将帮助您查看代码何时被视为代码以及 html 何时被视为 html。
<script type="text/javascript">
var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })";
var availableIds = [];
@for(var i = 0; i < Model.Data.Count (); i++) {
<text>availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" });</text>
}
</script>
Update for latest version
更新最新版本
You can now use the @:
syntax for even more readability
您现在可以使用@:
语法来提高可读性
<script type="text/javascript">
var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })";
var availableIds = [];
@for(var i = 0; i < Model.Data.Count (); i++) {
@:availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" });
}
</script>