0

I have this HTML button

<li><button class="btn-link glyphicon glyphicon-heart-empty" style="text-decoration:none;"></button></li>

Where "Item" is an Item in Model

@foreach (var Item in Model)

I am looking for one field in item "Item" that is Item.ID

I want to change the button class from

class="btn-link glyphicon glyphicon-heart-empty"

To this

class="btn glyphicon glyphicon-heart"

If the value is of ID is equal to 1

But I don't want to create an additional button using IF/ELSE statement such as

@if (Item.ID == 0)
  {
 <li><button class="btn-link glyphicon glyphicon-heart-empty" onclick="Run(@Item.ID)"></button></li>                    
  }
  else
  {
 <li><button class="btn glyphicon glyphicon-heart" onclick="Run(@Item.ID)"></button></li>
 }

Is there a better way to achieve this so I only change the CLASS

Cheers

1
  • You wrote same thing twice, "from class="btn-link glyphicon glyphicon-heart-empty" to class="btn-link glyphicon glyphicon-heart-empty"" Commented Oct 13, 2017 at 6:40

2 Answers 2

3

You can use ternary operator here.

class="btn-link glyphicon @(Item.ID == 0 ? "glyphicon-heart-empty" : "glyphicon-heart")"
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers for the solution
0

You cant also try this:

@{string glyphicon = string.empty}
@foreach (var item in Model)
{
    { glyphicon = item.Id == 0 ? "btn-link glyphicon glyphicon-heart-empty"
                               : "btn-link glyphicon glyphicon-heart";
    }

    <li>
        <button class="btn-link glyphicon @glyphicon" onclick="Run(@item.ID)"></button>
    </li> 
}

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.