有关在剃刀嵌套code座问题的声明 [英] Question about nested code block declarations in Razor

查看:199
本文介绍了有关在剃刀嵌套code座问题的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级从MVC 1一个项目,MVC 3,现在我尝试剃刀。

I've recently upgraded a project from MVC 1 to MVC 3 and now I'm trying out Razor.

在一个视图中,我有一个foreach code座,但if语句似乎并不想在它前面的@嵌套。

In one View, I have a foreach code block, but the nested if statement does not seem to want the @ in front of it.

我原来的code是:

@foreach(var r in Model.Results) 
{
    string css = r.Result.Count() > 0 ? "fail" : "pass";

    <p class="@css"><strong>@r.Description</strong></p>

    @if(r.Result.Count() > 0) 
    {
        <p>Count: @r.Result.Count()</p>
        <table>
            <thead>
                <tr>
                    <th>ID</th><th>Title</th><th>Description</th>
                </tr>
            </thead>
            <tbody>
            @foreach(var e in r.Result) {
                <tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
            }
            </tbody>
        </table>
    }
}

如果关键字后@字符意外:

我会用@if,说一个运行时错误。一旦进入code,你不需要preFIX结构,如如果和@。

I'll get a runtime error with @if that says: Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@".

如果我删除了@ code运行正常。我预计需要的@因为HTML立即preceding它。什么混淆我更多的是,我确实需要嵌套的foreach前的@。什么是规则在起作用吗?

If I remove the @ the code runs fine. I expected to need the @ because of the HTML immediately preceding it. What confuses me more is that I do need the @ before the nested foreach. What are the rules in play here?

推荐答案

嵌套的的foreach 是HTML里面的(这恰好是其他code内)

The nested foreach is inside of HTML (which happens to be inside of other code).

要从标记去的为code,你需要一个 @ 。结果
这只是在不必要时的直接的嵌套code块。

To go from markup to code, you need an @.
It's only unnecessary when directly nesting code blocks.

这篇关于有关在剃刀嵌套code座问题的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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