4

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">
1
  • 1
    Just a note. If you know the condition, why not handle this in the controller and return the proper value in the Model for the View. Commented Dec 17, 2013 at 18:23

4 Answers 4

9

@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" : "")'>
Sign up to request clarification or add additional context in comments.

1 Comment

No need for single-quotes. regular double-quotes will do.
0

I believe you may be wanting to do this:

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

2 Comments

@user3112333, if this fixed it for you please consider up voting and marking it as the answer. I'm glad I could be of assistance!
Thank you, but this way you suggested did not work. Do you know any other solution?
0

you can add javascript code

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

1 Comment

Thank you! So will works! But I would not use javascript at the moment. Do you know any other solution?
0

This should do it:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.