简单的if / else剃刀语法 [英] Simple If/Else Razor Syntax

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

问题描述

我试图做一个简单的if / else内与此code一个foreach:

I'm trying to do a simple If/Else within a foreach with this code:

@{
var count = 0;
foreach (var item in Model)
{
    if (count++ % 2 == 0)
    {
        @:<tr class="alt-row">
    } else { 
        @:<tr>
    }
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.Truncate(item.Details, 75)
        </td>
        <td>
            <img src="@Url.Content("~/Content/Images/Projects/")@item.Images.Where(i => i.IsMain == true).Select(i => i.Name).Single()" 
                alt="@item.Images.Where(i => i.IsMain == true).Select(i => i.AltText).Single()" class="thumb" />
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ProjectId }) |
            @Html.ActionLink("Details", "Details", new { id = item.ProjectId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ProjectId })
        </td>
    </tr>
}
}

我得到一个解析错误遇到结束标记TR没有匹配的开始标记。是你的开始/结束标记适当的平衡?好像if语句不想要'的工作。

I get a parse error "Encountered end tag "tr" with no matching start tag. Are your start/end tags properly balanced?". Seems like the if statement doesn't wanna' work.

推荐答案

只需使用此为结束标记:

Just use this for the closing tag:

  @:</tr>

,并留下您的if / else原样。

And leave your if/else as is.

好像if语句不想要'的工作。

Seems like the if statement doesn't wanna' work.

它工作正常。你在2语言的空间在这里工作,似乎只有不​​正确越过边界分割开/关三明治。

It works fine. You're working in 2 language-spaces here, it seems only proper not to split open/close sandwiches over the border.

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

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