如何在if语句中关闭/重新打开标记

Jus*_*elf 1 c# asp.net-mvc razor visual-studio-2012

我无法弄清楚这个剃刀代码有什么问题.

IDE告诉我,我的第一个<ul>没有匹配的结束标记.它还表示结束标记没有匹配的开始标记.它也不会将多行识别为C#代码,而是将其视为常规文本.

IDE投诉是对违规行的评论.

<div class="list-container">
    @{
        int i = 0;   

        <ul> //no matching ending tag
            @foreach (var item in Model.Items)
        {
            if (i % 8 == 0)
            {
        </ul> //no matching start tag
        <ul> //"Text is not allowed between the opening and closing tags for element ul"
            } //IDE doesn't recognize this closing brace as code... see it as text

        <li>@item.ContentItem.Title.Value</li>

            i++; //IDE doesn't recognize this closing brace as code... see it as text
        } //IDE doesn't recognize this closing brace as code... see it as text
        </ul>
    }
</div>
Run Code Online (Sandbox Code Playgroud)

当执行视图时,我得到了你在下面的时候所期望的编译错误}:"代码块缺少一个结束"}"字符."

一旦我</ul><ul>if语句中删除了from ,视图就会编译并执行.

有趣的是,当我点击其中一个ul标签时,也会突出显示正确的开始或结束标签.

显然,我做错了什么.我没有正确使用Razor吗?

Pat*_*lis 8

您可以使用它@:明确定义内容.以下内容不会给我的系统带来编译错误:

<div class="list-container">
    @{
        int i = 0;   

        @:<ul>
            @foreach (var item in Model.Items)
        {
            if (i % 8 == 0)
            {
        @:</ul>
        @:<ul>
            }

        <li>@item.ContentItem.Title.Value</li>

            i++;
        }
        @:</ul>
    }
</div>
Run Code Online (Sandbox Code Playgroud)

注意:您的帖子中也有不正确的注释语法,但我假设这只是针对SO示例.