简体   繁体   English

剃刀视图在ASP.NET中使用@

[英]razor view using @ in ASP.NET

I know we use @ symbol to switch between C# code and html code, but for code like this below: 我知道我们使用@符号在C#代码和html代码之间切换,但是对于下面的代码:

@for (int i = 1; i <= 10; i++)
{
    @i
}

why can we still place @ before i as @i? 为什么我们仍可以在@之前将@放置为@i? Since there is no html element here,so there is no switching and i is the c# code, if we just use "i" instead of "@i", we should get the same output? 因为这里没有html元素,所以没有切换,而我是c#代码,如果我们仅使用“ i”而不是“ @i”,我们应该得到相同的输出吗?

Another question is : 另一个问题是:

@for (int i = 1; i <= 10; i++)
{
    <b>@i</b> 
    if (i % 2 == 0)
    {
        <text> - Even </text>
    }
    else
    {
        <text> - Odd </text>
    }
    <br />
}

why we don't need to place @ before if since < /b> before if indicates that it is current in html mode, and then we need to do like 为什么我们不需要将@放在if之前,因为</ b>在if之前表示它在html模式下是当前的,所以我们需要这样做

@if (i% 2 == 0)
{
   ...
}

In your first example, the @i is an implicit expression where you are telling razor to output the value of i on the page. 在你的第一个例子中, @i是你告诉剃刀输出的值隐式表达i的页面上。 Removing the @ will turn it into a C# statement which will be an invalid syntax. 删除@会将其转换为C#语句,这将是无效的语法。

The @i in enclosed in your <b> tag is also an implicit expression. 包含在<b>标记中的@i也是一个隐式表达式。

Now your if inside your for block does not need an @ since it is inside the for code block. 现在,您的for块内的if不需要@因为它位于for代码块内。 If it wasn't, you would need to add one before it (ie, @if ... ). 如果不是,则需要在它之前添加一个(即@if ... )。

References: 参考文献:

In the Razor view engine @ symbol is used to write embedded C# code within the HTML code it is like mixing C# and HTML code to render a template. 在Razor视图引擎中, @符号用于在HTML代码内编写嵌入式C#代码,就像将C#和HTML代码混合在一起以呈现模板一样。

Question 1 - Ans : 问题1-答

@for (int i = 1; i <= 10; i++)
{
    @i
}

In the above code snippet, a HTML text will be directly placed in the output stream since it is rendering the i value directly in the HTML. 在上面的代码片段中,HTML文本将直接放置在输出流中,因为它直接在HTML中呈现i值。

without the @i the code isn't valid because just i in C# isn't valid code neither it is in HTML so you would get a compilation error when the Razor view is parsed. 没有@i的代码是无效的,因为仅C#中的i既不是有效的代码,也不是HTML的代码,因此在解析Razor视图时会出现编译错误。

Quoestion 2 - Ans : 问题2-答案

@for (int i = 1; i <= 10; i++)
{
    <b>@i</b> 
    if (i % 2 == 0)
    {
        <text> - Even </text>
    }
    else
    {
        <text> - Odd </text>
    }
    <br />
}

The Razor view engine is smart enough to know whether the embedded C# code is valid or not when it parses it. Razor视图引擎足够聪明,可以在解析嵌入式C#代码时知道它是否有效。

What goes on here is that an embedded code block is started with using the @ sign later on any valid C# code doesn't need to have @ sign again before it because it is already inside the embedded code block, now just by properly closing the HTML tags or writing valid HTML the Razor view engine will render the template appropriately. 此处发生的情况是,嵌入式代码块以后可以在任何有效的C#代码上使用@符号开始,而无需再使用@符号,因为它已经在嵌入式代码块中,现在只需正确关闭HTML标签或编写有效的HTML,Razor视图引擎将适当地渲染模板。

If you want to render a C# variable inside a HTML element then again you need to use the @ sign because the embedded code block breaks at the HTML tag and then continues again after that HTML element. 如果要在HTML元素内呈现C#变量,则再次需要使用@符号,因为嵌入的代码块在HTML标记处中断,然后在该HTML元素之后再次继续。

In Razor, @ is used for two different purposes: 在Razor中,@用于两个不同的目的:

  • To denote the start of a code block. 表示代码块的开始。 This is like <% %> in Web Forms. 就像Web窗体中的<%%>。

  • To denote the inline expressions. 表示内联表达式。 This is like <%: %> in Web Forms. 就像Web窗体中的<%:%>。

1st question 第一个问题
@for {...} denote for a block of code, @i denote for the inline expression, telling razor to output the value of i on the page. @for {...}表示用于代码块,@i表示用于内联表达,告诉剃刀输出i的页面上的值。
If you don't place @ before i, your code will be turned into invalid syntax. 如果您未在@之前放置@,则您的代码将变成无效的语法。

2nd question 第二个问题
We need to place @ before i in the expression @i because it is an inline expression, telling razor to output the value of i with bold character on the page. 我们需要将@放在i之前的@i表达式中,因为它是一个内联表达式,告诉razor在页面上输出粗体i的值。
However, on next statement, we don't need to place @ before if , because it's still inside a block of code @for {...} 但是,在下一条语句中,我们不需要将@放在if之前,因为它仍在代码块内@for {...}

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

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