Lodder
Lodder

Reputation: 19743

.NET get variable value inside HTML attribute

This is my first time using .NET so please excuse my ignorance.

I'm trying to change the value of the class atribute in a HTML element, depending on some if/else statements, like so:

@if (item.HasAir)
{
    var icon = "fa fa-plane";
    var circleColour = "icon-bg-blue";
}
else if (item.HasCar)
{
    var icon = "fa fa-car";
    var circleColour = "icon-bg-cyan";
}

<span class="icon-cirle @circleColour" aria-hidden="true">
    <span class="@icon"></span>
</span>

However when refreshing the page in my browser, I'm getting a Compilation Error:

The name 'circleColour' does not exist in the current context

Looking at the code above, can anyone explain what I'm doing wrong?

Other info:

Upvotes: 0

Views: 82

Answers (1)

Lodder
Lodder

Reputation: 19743

My mistake, I didn't initially declare a default value, in case the if/elseif statements weren't met, which in my case, they weren't.

The following seemed to solve the issue:

@{
    var icon = "fa fa-plane";
    var circleColour = "icon-bg-blue";
}

@if (item.HasCar)
{
    icon = "fa fa-car";
    circleColour = "icon-bg-cyan";
}
...

Upvotes: 1

Related Questions