0

我想根据条件是否为真更改行的背景颜色。我尝试了以下方法,但它不喜欢语法。我尝试了几种不同的方法,但它不喜欢其中任何一种

@<tr class="@if (item.ActualDeliveries <> item.ExpectedDeliveries ) 
             then string.empty 
             else "style=background-color:Yellow" 
            End if)"> 

这是我得到的错误:

“If”块未终止。所有“If”块语句必须以匹配的“End If”终止。string.empty 不是 else 语句中的方法,它说“语法错误”

- 更新

即使使用以下语句,剃须刀也不会输出任何内容。但是,如果我在“</tr>”结束标记之后使用“end if”,它就可以工作。

If True Then
    @:<tr>
    End If
        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.StartDate)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = currentItem.ID})
        </td>
    </tr>
4

2 回答 2

1

尝试这个:

<tr class="@(If(item.ActualDeliveries = item.ExpectedDeliveries, "style=background-color:Yellow;", String.Empty))"
于 2013-07-16T14:17:53.833 回答
1

与其尝试在标签本身中解析 Razor,不如尝试以下操作:

@If item.ActualDeliveries <> item.ExpectedDeliveries Then
<tr>
Else
<tr style="background-color:yellow"> 
End If

您可能会收到来自设计器的警告,但您可以忽略这些警告 - 将在运行时生成正确的 HTML。此外,如果实际编译,您的标记将按原样创建类似的内容:

<tr class="style=background-color:yellow"> 

这显然是不正确的。

于 2013-05-16T03:17:05.363 回答