Search code examples
asp.net-corerazorconditional-operator

Inserting HTML into Razor view based on conditional value


 @(item.ID == 0 ? "" : <i class="fas fa-bus"></i>)

When I try the above statement, Razor doesn't like it, It looks like valid C# code. What do I need to do to wrap my html tags so that it will be rendered in my UI if the ID is 0?


Solution

  • try this:

    @if (item.ID == 0)
    {
        @:<i class="fas fa-bus"></i>
    }
    

    In razor pages, anything that follows the @: will be treated as content block.