Basic if
it's green
@{
var aValue = "green";
}
@if(aValue == "green") {
<span>it's green </span>
}
If-Else
it's blue
@{
var val2 = "blue";
}
@if(val2 == "green") {
<span>it's green </span>
} else {
<span>it's blue</span>
}
Using if-else and <text>
tags
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
it's not pink
@{
var val4 = "orange";
}
@if(val4 != "pink") {
<text>it's not pink</text>
}
Quick if-else using Ternary operator ? :
it's not red
@{
var val3 = "blue";
}
@(val3 == "red" ? "it's red" : "it's not red")
@inherits Custom.Hybrid.Razor14
<!-- unimportant stuff, hidden -->
<div class="row">
<div class="col-lg-7">
<h2>Using if and if-else</h2>
</div>
@Html.Partial("../shared/_DefaultInfoSection.cshtml")
</div>
<h3>Basic <code>if</code></h3>
@{
var aValue = "green";
}
@if(aValue == "green") {
<span>it's green </span>
}
<hr>
<h3>If-Else</h3>
@{
var val2 = "blue";
}
@if(val2 == "green") {
<span>it's green </span>
} else {
<span>it's blue</span>
}
<hr>
<h3>Using if-else and <code><text></code> tags</h3>
<p>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</p>
@{
var val4 = "orange";
}
@if(val4 != "pink") {
<text>it's not pink</text>
}
<hr>
<h3>Quick if-else using Ternary operator <code>? :</code></h3>
@{
var val3 = "blue";
}
@(val3 == "red" ? "it's red" : "it's not red")
<!-- unimportant stuff, hidden -->