Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
like image 967
Accorsi Avatar asked Jan 11 '23 16:01

Accorsi


1 Answers

@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" : "")'>
like image 68
Becuzz Avatar answered Jan 21 '23 23:01

Becuzz