"RZ1010 error from nested @{ } inside a Razor code block"
▸ SYMPTOM
Adding a @{ var x = ...; } block inside an existing @if(...){ } or @for(...){ } block produces:
RZ1010: Unexpected "{" after "@" characterThe code looks reasonable — you just wanted a local variable — but the Razor compiler rejects it.
▸ CAUSE
The @{ } syntax is a markup-to-code transition. It tells the Razor parser "switch from HTML context into C# context." But inside an @if or @for block, you are already in C# context. Writing @{ } there is a double-transition that the parser cannot resolve.
@if (showPanel)
{
@{ var label = "Hello"; } // RZ1010 — already in code context
<span>@label</span>
}▸ FIX
Option A — Hoist the setup block above the wrapping code block:
@{
var label = showPanel ? "Hello" : "";
}
@if (showPanel)
{
<span>@label</span>
}Option B — Drop the @ and use a bare block (you're already in C#):
@if (showPanel)
{
{
var label = "Hello";
// ... use label
}
<span>Done</span>
}In most cases, option A is cleaner — hoist the variable declaration to before the conditional.
▸ WHY IT WORKS
Razor's parser tracks whether you're in markup or code context. @{ } is only valid as a transition from markup into code. Once you're inside @if { ... }, you're already in code — writing bare C# statements, var declarations, and method calls directly. The @ prefix is only needed to output an expression back into markup (e.g., <span>@label</span>), not to open a new code block.