If Statement (For CSS Class) on Razor Views

A simple solution would be something like this:

@foreach (var item in Model) 
{
    var style = (item.status == "Unread") ? "font-weight:bold" : "";

    <tr style="@style">
        ...
    </tr>
}

But note, it's generally cleaner to have a separate CSS class, then directly decorate the elements the appropriate class based on its status. For example:

/* css */
tr.status-unread { font-weight: bold; }
...

/* razor */
@foreach (var item in Model) 
{
    <tr class="[email protected]()">
        ...
    </tr>
}

Another simpler solution would be this,

  1. With single condition:

    @foreach (var item in Model) 
    {
        <tr style="@Html.Raw(item.status == "Unread" ? "font-weight:bold" : "")">
            ...
        </tr>
    }
    
  2. OR you can set CSS class as you asked with multiple conditions if any,

    @foreach (var item in Model) 
    {
        <tr class="@Html.Raw((item.status == "Unread" && item.subject == "Hello World") ? "alert-success" : "")">
            ...
        </tr>
    }
    

This way I used in my project.

You can use unitary operator like below

<td style="color:'@(item.ChangeRate > 0 ? "red" : "blue")'">
 <i class="fa" class="@(item.ChangeRate > 0 ? "fa-caret-up" : "fa-caret-down")"></i>
</td>