What is causing __builder does not exist in current context error from compiler?

Viewed 3290

"Error CS0103 The name '__builder' does not exist in the current context"

Here is my code that causes the error:

@code {
    public void Calc()
    {
        for (int i = 0; i < 55; )
        {
           <div class="alert-info">

               <h3>This is the number" &nbsp;@i</h3>

           </div>
        }
    }
}

Error message from compiler

4 Answers

You can't render HTML in @code or @functions block. You can do this in a @{ } code bock.

As per ltwlf's answer, HTML markup is not allowed within the @Code block. I received the same error, but I was positive that I had no HTML within my @code block. However, after careful review I noted that there were HTML comment tags <!-- ... --> inside my @code block. This displays correctly as green comments and is easily missed - Therefore carefully review your @code block for any HTML markup if you get this error.

Members of the component class are defined in one or more @code blocks. In @code blocks.

Component members are used in rendering logic using C# expressions that start with the @ symbol. For example, a C# field is rendered by prefixing @ to the field name.(more info)

The RenderFragment delegate must accept a parameter called __builder of type RenderTreeBuilder so that the Razor compiler can produce rendering instructions for the fragment.

Sample1:

@using Microsoft.AspNetCore.Components.Rendering

@{ load1(__builder); }
@code {
    void load1(RenderTreeBuilder __builder)
    {
        @for (int i = 0; i < 1; i++)
        {
            <div class="alert-info">
                <h3>This is the number @i</h3>
            </div>
        }
    }
}

Sample2:

<button class="btn btn-success" @onclick="()=>Calc()">Action</button>
@((MarkupString)myMarkup)
@code {
    string myMarkup = "";
    void Calc()
    {
        @for (int j = 0; j < 55; j++)
        {
            myMarkup += getMarkup(j);
        }
    }

    string getMarkup(int j) {
        var myMarkup = "<div class='alert-info'>";
        myMarkup += $"<h3>This is the number {j}</h3>";
        myMarkup += "</div>";
        return myMarkup;
    }
}
Related