2 May 2026

TIP : Blazor Unexpected closing tag ‘div’ with no matching start tag.

Quick Monday morning tip. In a loop, I needed to open a <Div> and close that block after several elements in the loop. However Razor does not seem to be able to understand the C# conditions during its code analysis.

[cc lang=”csharp” escaped=”true” width=”690″ line_numbers=”0″ theme=”vsdark”]
@foreach (var item in myObjects)
if (myCondition)
{
<Div>
}
else
{
</Div>
}

// …
}
[/cc]

Well, we can’t compile the code. We get this error “ RZ9981 Unexpected closing tag ‘div’ with no matching start tag.

The little trick is to use either
[cc lang=”csharp” escaped=”true” width=”690″ line_numbers=”0″ theme=”vsdark”]
@((MarkupString)”

“)
[/cc]
or
[cc lang=”csharp” escaped=”true” width=”690″ line_numbers=”0″ theme=”vsdark”]
@:

[/cc]

Leave a Reply