Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CSS class name dynamically?

I want to do something like this:

View

<td class="[email protected]">
      @Model.Description
</td>

CSS

<style type="text/css">
    .status_True
    {
        background-color: Red;
    }
    .status_False
    {
        background-color: Green;
    }
</style>

@Model.IsActive is return True or False. Of course this code is not working.

Html output of class is : class="[email protected]"

My expected output is : class="status_True"

Is this possible to do something like this logic? If is possible, How can I do? Or another way to change css class? There may be different ways.

Thanks.

like image 608
AliRıza Adıyahşi Avatar asked Dec 12 '25 17:12

AliRıza Adıyahşi


1 Answers

Razor syntax takes some getting used to. Use parenthesis to get the actual property value:

<td class="status_@(Model.IsActive)">

Here's a quick reference from Phil Haack

like image 152
jrummell Avatar answered Dec 14 '25 09:12

jrummell