Search code examples
c#htmltagsblazor

Blazor - HTML tag inside of c# bound bool switch


I want to add a icon into a button context depending on a bool.

How to implement a HTML tag into a c# bound bool switch?

<button type="button"> 
   @(SwitchBool? "Save" : @"<i class='\u0022'fa fa-pen'\u0022'></i>")
</button>

Solution

  • You can do it like this:

    <button type="button" @onclick="() => SwitchBool = !SwitchBool"> 
       @if(SwitchBool)
       {
           <text>Save</text>
       }
       else
       {
           <i class="fa fa-pen"></i>
       }
    </button>
    
    @code {
        private bool SwitchBool;
    }