混合JavaScript和剃刀语法 [英] Mixing JavaScript and Razor syntax

查看:138
本文介绍了混合JavaScript和剃刀语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何剃刀和JavaScript混合在一起。我发现是这样的:

I was looking for how to mix razor and JavaScript together. I found something like this:

<script type="text/javascript">
var currentUser = null;
@if (User.Identity.IsAuthenticated) {
    <text>
        currentUser = '@User.Identity.Name';
    </text>
}
</script>

为什么叫变量的currentUser 包围&LT;文本&GT;&LT; /文本&GT; 标记?什么是&LT的含义;文字和GT; 标记?如果我们忽略的文本标签,然后任何错误发生的?

Why is the variable called currentUser surrounded with <text></text> tag? What is the meaning of <text> tag? If we omit that text tag then any error occur?

推荐答案

究其原因&LT;文本&GT; ...&LT; /文本&GT; 标签是解决问题的出现的作为两种语言混合在一起,在许多情况下,语义上相同的结果歧义。

The reason for the <text>…</text> tags is to solve the issue of ambiguities that arise as a result of your mixing two languages together that are semantically identical in many cases.

如果你是if语句的语法剃刀的C#中指定的动态Java脚本逻辑,你有Java脚本如果这些块语句,如何将它区分两者之间的范围内?

If you were to specify dynamic java script logic within the Razor syntax's C# if statements, and you had java script if statements in those blocks, how would it differentiate the context between the two?

问题是:它不能;赔率是,程序员可以没有。在&LT;文本&GT; ...&LT; /文本&GT; 标记是一个特殊的剃刀块(内C#块只)风格的转义,说:这绝对是(可能)不C#code,它是在此块。

The problem is: it can't; odds are, programmers can't either. The <text>…</text> tag is a special Razor block (inside C# blocks only) style escape that says: this is definitely (probably) not C# code that is in this block.

事情变得有点冒险,当你意识到,你可以添加的其他的逃脱&LT内部;文本&GT; ...&LT; /文本&GT; 块。任何事情一样,虽然你会谨慎使用。如果你遇到了不少麻烦混合两种并不能找出其中添加文本块,赔率是你不应该使用它(在生产中),直到你更好地理解它来。

Things get a bit dicey when you realize you can then add another escape inside the <text>…</text> block. Like anything though you would use it with caution. If you're having a lot of trouble mixing the two and can't figure out where to add your text blocks, odds are you shouldn't be using it (in production) until you understand it better.

这篇关于混合JavaScript和剃刀语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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