C# 根据VisualStudio的说法,条件剃须刀括号不能相加

C# 根据VisualStudio的说法,条件剃须刀括号不能相加,c#,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net Mvc,Razor,Asp.net Mvc 5,我有一个Razor视图,其中我使用引导作为我的设计/前端基线。现在我想创建一个典型的行/col-sm-6,然后为每一个项目创建一个新行 但是,当我执行以下标记时,我会得到一个: Additional information: The if block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this bloc

我有一个Razor视图,其中我使用引导作为我的设计/前端基线。现在我想创建一个典型的行/col-sm-6,然后为每一个项目创建一个新行

但是,当我执行以下标记时,我会得到一个:

Additional information: The if block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
我只是不明白为什么我会犯这个错误。如果您在下面看到我的标记,那么应该是正确的
{
}

知道我为什么会出错吗

@{ int index = 0; }
@if (Model != null && Model.Items != null)
{
    <div class="projects">

    @foreach (var project in Model.Items)
    {
        if (index % 2 == 0)
        {
            <div class="row">
        }

        <div class="col-sm-6 project">
            @if (!string.IsNullOrEmpty(project.Image))
            {
                <img src="@project.Image" />
            }

            <div class="project_title">@project.Title</div>

            @if (project.UserSnippet != null)
            {
                <img src="@project.UserSnippet.ProfilePic" class="post_image" />
                @Html.Partial("_UserSnippet", project.UserSnippet)
            }


            @if (!string.IsNullOrEmpty(project.DescriptionSnippet))
            {
                <p class="project_description">@project.DescriptionSnippet</p>
            }

            <a href="/p/@project.Alias">Read more about project</a> @if (Model.ShowEditLink)
            {
                <a href="@Url.Action("EditProject", "Project", new { project_id = project.Id})">Edit project</a>
            }
        </div>

        @if (index%2 == 0)
        {
            </div>
        }

        }

    </div>
}
@{int index=0;}
@if(Model!=null&&Model.Items!=null)
{
@foreach(Model.Items中的var项目)
{
如果(索引%2==0)
{
}
@如果(!string.IsNullOrEmpty(project.Image))
{
}
@项目名称
@if(project.UserSnippet!=null)
{
@Html.Partial(“\u UserSnippet”,project.UserSnippet)
}
@如果(!string.IsNullOrEmpty(project.DescriptionSnippet))
{

@project.DescriptionSnippet

} @if(Model.ShowEditLink) { } @如果(索引%2==0) { } } }
损坏的Razor解析器是此问题的根源。它无法知道您的开始
div
有相应的结束
div
,因为它们被包装在运行时计算的
if
语句中。如果出于某种原因,开始的if语句计算结果为true,结束的if语句计算结果为false,那么最终将得到一个中断的标记。但在你的情况下,你知道这不会发生,因为他们有相同的条件

if (index % 2 == 0)
{
    @:<div class="row">
}
if(索引%2==0)
{
@:
}
在这里:

 @if (index % 2 == 0)
 {
     @:</div>
 }
@if(索引%2==0)
{
@:
}

错误是正确的,您有1个额外的}。看起来倒数第二个是您应该删除的。不,错误根本不正确。是坏了的剃须刀,先生,你真了不起。谢谢@达林德米特罗夫:这真的是“破碎”还是“过于保守”?