7

"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

H H
  • 263,252
  • 30
  • 330
  • 514
JakeL
  • 191
  • 1
  • 6
  • 18
  • 1
    It appears that this is a known issue, but still don't know how to resolve https://github.com/dotnet/aspnetcore/issues/13275 – JakeL Jun 07 '20 at 06:21

5 Answers5

19

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

ltwlf
  • 219
  • 3
  • 5
  • Thank you, I had been writing code int he main section and swppied it over to @code and thought I swapped all the html out. Once I knew what to look for I was able to fix my issue quickly. – Jeremy D Nov 18 '21 at 17:56
  • As for @functions MS says otherwise https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-6.0#functions but in fact you are right -- one has to stick with "@{ }" and drop all "private/public/whatever" modifiers making functions local. – greenoldman May 28 '22 at 16:20
1

The correct answer is at this SO link.

In short you cannot mix markup with C# code inside @code block.

Consider make a component for reusing the markup.

MasterWil
  • 891
  • 8
  • 24
1

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;
    }
}
Sajad Mirzaei
  • 2,635
  • 1
  • 11
  • 13
0

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.

Daniël J.M. Hoffman
  • 1,539
  • 10
  • 16
0

In order to use the "onclick" within a razor page you must prefix it with the @ symbol.

Event Handling

Event handling ASP.NET Core 7

Todd
  • 119
  • 10