1 May 2024

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.

@foreach (var item in myObjects)
    if (myCondition)
    {
        <Div>
    }
    else
    {
        </Div>
    }

    // ...
}

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

@((MarkupString)"<div>")

or

@:</div>

Leave a Reply