Accorsi
Accorsi

Reputation: 43

Place a css class in a div using MVC and Razor

I have used following code to add css class:

<div id="123" class="tab-pane @{ if (some_condition) { Html.Raw("active"); }; } ">

But it did not work.

I hope for this result:

<div id="123" class="tab-pane active">

Upvotes: 4

Views: 6230

Answers (4)

James Harcourt
James Harcourt

Reputation: 6379

This should do it:

<div id="123" class="tab-pane @if (some_condition) { <text>active</text>  }">

Upvotes: 0

Becuzz
Becuzz

Reputation: 6866

@MichaelPerrenoud's answer is close. You need to wrap the whole conditional in parenthesis.

Using @() tells razor to output a string. So

<div id="123" class='tab-pane @(condition ? "active" : "")'>

Upvotes: 9

user2167382
user2167382

Reputation: 346

you can add javascript code

    <script>
       @if(true)
       $('#123').addClass("active");
    </script>

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

I believe you may be wanting to do this:

<div id="123" class='tab-pane @some_condition ? "active" : ""'>

Upvotes: 0

Related Questions