Asp.Net MVC3 RC剃刀观点:语法内与LT嵌入code; JavaScript的>块 [英] Asp.Net MVC3 RC Razor views : syntax for embedding code inside <javascript> block

查看:108
本文介绍了Asp.Net MVC3 RC剃刀观点:语法内与LT嵌入code; JavaScript的>块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题嵌入C#code一个JavaScript块中是 MVC3 RC1 的Razor视图。我应该使用什么语法,使这方面的工作?我得到的警告如。预计其他中isOk变量不变。 (没有文字标签,下面code不被认为是JavaScript的)

  @ {布尔isOk = TRUE;}<脚本类型=文/ JavaScript的>
    变种TMP1 =;
    @if(isOk)
    {
        <文字和GT;
            变种TMP =; //这应该是PTED如JavaScript间$ P $
        < /文字和GT;
    }
< / SCRIPT>


解决方案

您需要的<文本> 标记来表示的内容是不是在服务器上进行评估。例如:

  @ {
    VAR isOK = TRUE;
}<脚本类型=文/ JavaScript的>
    变种TMP1 =;
    @if(isOK)
    {
        <文字和GT;
            VAR TMP =富;
        < /文字和GT;
    }
< / SCRIPT>

在结果页输出:

 <脚本类型=文/ JavaScript的>
    变种TMP1 =;
    VAR TMP =富;
< / SCRIPT>

至于预计恒警告而言,好了,剃刀智能感知仍处于测试阶段所以它远非完美。这只是你可以放心地忽略警告。最重要的是什么是应用程序的工作原理,并发出有效的HTML。希望这是一些将固定在最终产品中。从个人的经验,我相信唯一的警告是由C#编译器发出的之一。当我在视图中我的工作我放心地忽略所有蹩脚的警告,Visual Studio中发出,因为我知道这是不对的。

Having trouble embedding c# code inside a javascript block with MVC3 RC1 razor view. What syntax should I use to make this working? I get warnings eg. Expected constant on the isOk variable among other. (Without text tag, the following code is not recognized as javascript)

@{bool isOk = true;}

<script type="text/javascript">
    var tmp1 = "";
    @if (isOk)
    { 
        <text>
            var tmp=""; // this should be interpreted as javascript
        </text>       
    }
</script>

解决方案

You need the <text> tag to indicate that the contents is not evaluated on the server. For example:

@{
    var isOK = true;
}

<script type="text/javascript">
    var tmp1 = "";
    @if (isOK)
    { 
        <text>
            var tmp = "foo";
        </text>       
    }
</script>

will output in the resulting page:

<script type="text/javascript">
    var tmp1 = "";
    var tmp = "foo";
</script>

As far as the Expected Constant warning is concerned, well, Razor Intellisense is still in beta so it's far from perfect. It's just a warning you can safely ignore. The important thing is what is that the application works and emits valid HTML. Hope this is something that will be fixed in the final product. From personal experience the only warnings I trust are the one emitted by the C# compiler. When I am working in a view I safely ignore all the crappy warnings that Visual Studio emits because I know that it is wrong.

这篇关于Asp.Net MVC3 RC剃刀观点:语法内与LT嵌入code; JavaScript的&GT;块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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