简体   繁体   English

MVC剃刀代码发行

[英]MVC Razor Code Issue

I'm trying to do something like the following: 我正在尝试执行以下操作:

<tbody class="searchable">
    @foreach (var item in Model)
    {
        if (item.Account.AccountName != "xyz")
            <tr class="danger">
        else
            <tr>

            <td>
                @Html.DisplayFor(modelItem => item.LocationName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
        </tr>
    }
</tbody>

The compiler doesn't like the if statement where I'm trying to create a row tag dynamically. 编译器不喜欢试图动态创建行标记的if语句。 The error I'm getting says the @foreach block is missing a close '}'. 我收到的错误消息说@foreach块缺少结束符'}'。 My guess is that the 'if' statement is being misinterpreted. 我的猜测是“ if”陈述被误解了。 Can someone suggest how I can fix this? 有人可以建议我如何解决此问题吗?

You can just avoid the if, and do something like: 您可以避免if,然后执行以下操作:

<tr class='@(item.Account.AccountName != "xyz" ? "danger" : "")'>
    <td>
        @Html.DisplayFor(modelItem => item.LocationName)
    </td>
  <td>
    @Html.DisplayFor(modelItem => item.City)
  </td>
</tr> 

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

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