the-a-monir
the-a-monir

Reputation: 177

Set css multiple class runtime in tag in asp.netcore mvc page

I have a cshtml page where at page level i have a variable active having css class names. I want to set this variable in a button tag in the same page.

@{
    ViewData["Title"] = "Title";
    var active ="nav-link active";   
}   

<button class=@active id="nav-experience-tab" ... ...
    

But the output is:

<button class="nav-link" active="" id="nav-experience-tab" 

Second way:

var active ="class='nav-link active'";
<button @active id="nav-experience-tab" ... ... 

output is:

<button class="'nav-link" active&#x27;="" id="nav-experience-tab"

Third way:

var active ="class=nav-link active";
<button @active id="nav-experience-tab" ... ... 
    

output is:

<button class="nav-link" active="" id="nav-experience-tab" 

Is there anyway to get the below?

<button class="nav-link active" id="nav-experience-tab"

Upvotes: -1

Views: 105

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9943

You can set like this:

@{
    var active ="nav-link active";
}

<button class="@active" >click</button>

Demo

enter image description here

Upvotes: 2

Related Questions