简体   繁体   English

Razor语法和Javascript

[英]Razor Syntax and Javascript

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 Forms写到Razor的概念验证应用程序,只是为了我们可以评估它。

I've run into one problem so far that's making my head hurt..generating client-side Javascript... 到目前为止,我遇到了一个问题,即让我头疼...生成客户端Javascript ...

Web-Forms web的表单

<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 基隆

You need to wrap it in the pseudo element <text> . 您需要将其包装在伪元素<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#. 这会将解析器切换回html模式,然后它将解析javascript作为html的一部分,而不是c#。 The reason it happens is the @for() is ac# block and anything treated within is also considered c# until it's escaped by an html tag. 它发生的原因是@for()是ac#block,其中的任何内容也被认为是c#,直到它被html标签转义。 Since you probably don't want an html tag razor provides the <text> tag to switch modes. 因为你可能不想要一个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. 如果您注意到asp.net webforms中的差异,则结束<% for line with a %> ,这将使其退出c#模式。 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. 如果您下载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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM