0

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:

  • .NET Framework version: 4.0.x
  • ASP.NET version: 4.7.x
1
  • 2
    Your icon and circleColor variables are scoped inside the if statements - that is, they are out of scope when you reference them in your HTML. Commented Aug 29, 2017 at 15:11

1 Answer 1

1

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";
}
...
Sign up to request clarification or add additional context in comments.

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.