Skip to main content
Home  › ... Razor

Razor Basics Tutorials

Tutorial HomeRazor Basics
#2 Razor Conditions such as if, if-else and ? :

Razor Conditions such as if, if-else and ? :

⬇️ Result | Source ➡️

it's green
@inherits Custom.Hybrid.RazorTyped

@{
  var aValue = "green";
}
@if (aValue == "green") {
  <span>it's green </span>
}

⬇️ Result | Source ➡️

it's blue
@inherits Custom.Hybrid.RazorTyped

@{
  var val2 = "blue";
}
@if (val2 == "green") {
  <span>it's green </span>
} else {
  <span>it's blue</span>
}

This is important, if you have an code block like an if, and you don't want a span-tag or similar in the output

⬇️ Result | Source ➡️

it's not pink
@inherits Custom.Hybrid.RazorTyped

@{
  var val4 = "orange";
}
@if (val4 != "pink") {
  <text>it's not pink</text>
}

⬇️ Result | Source ➡️

it's not red
@inherits Custom.Hybrid.RazorTyped

@{
  var val3 = "blue";
}
@(val3 == "red" ? "it's red" : "it's not red")

 

#2 Razor Conditions such as if, if-else and ? :