SamJolly
SamJolly

Reputation: 6477

Trying to write some razor code to define a CSS class in a Razor view, pretty simple, but getting confused

I am trying to define a CSS class on a depending on a condition, in a razor view. Must be simple, but I am struggling with it.

Code so far is:

        <tr class="@{if (id == 0) {@:selected} else {@:notselected}}">

Obviously missing something simple....

Thoughts appreciated....

Upvotes: 2

Views: 69

Answers (2)

archil
archil

Reputation: 39501

What I do in that kind of situations is

@{
   string rowClass = id == 0 ? "selected" : "notselected";
}

<tr class="@rowClass">

Anyways, if you want that inline, you could use

 <tr class="@(id == 0 ? "selected" : "notselected")">

Upvotes: 2

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38488

Try this:

<tr class="@((id == 0) ? "selected" : "notselected")">

Upvotes: 2

Related Questions