-1

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"

1 Answer 1

2

You can set like this:

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

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

Demo

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.