please read and learn the razor syntax. razor syntax is a template language. when in razor mode the @ is used to switch to c# code mode. when in c# mode, a razor block (<tag>) converts back to razor mode.
@{ // define a c# code block
var foo = "bar";
<div> now in razor mode to add code need to go back to c# mode
@if (foo == "bar")
{
// in c# mode
<text>back in razor mode</text>
}
</div>
}
razor also parses the razor tags into a single expression. the tag must be complete. the tag can wrap code blocks and expressions, but can not span code blocks.
valid:
<div>
@if (...) {
...
} else {
...
}
</div>
invalid:
@if (...) {
// tag must be complete before returning to the if code context
<div>
} else {
</div>
}