Baahubali
Baahubali

Reputation: 4802

Conditional Highlighting row in MVC Razor VB.NET

I want to change the background color of a row based on if the condition is true. i have tried the following but it does not like the syntax. I have tried a few different ways but it does not like any of them

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

This is the error I get:

The "If" block was not terminated. All "If" block statements must be terminated with a matching "End If". string.empty is not a method in the else statement it says"Syntax error"

--Updated

The razor does not output anything even using the following statement. However if i use the "end if" after the "< /tr>" closing tag, it works.

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>

Upvotes: 0

Views: 2247

Answers (2)

Douglas Barbin
Douglas Barbin

Reputation: 3615

Try this:

<tr class="@(If(item.ActualDeliveries = item.ExpectedDeliveries, "style=background-color:Yellow;", String.Empty))"

Upvotes: 1

Tieson T.
Tieson T.

Reputation: 21191

Instead of trying to parse Razor within the tag itself, try something like this:

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

You may get warnings from the designer, but you can ignore those - the correct HTML will be generated at runtime. Also, your markup as-is would be creating something like this if it actually compiled:

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

which is obviously incorrect.

Upvotes: 1

Related Questions