3

"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

Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
Luke Vincent
  • 63
  • 1
  • 4
  • 16
  • 1
    It appears that this is a known issue, but still don't know how to resolve https://github.com/dotnet/aspnetcore/issues/13275 – Luke Vincent Jun 07 '20 at 06:21

3 Answers3

13

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

ltwlf
  • 151
  • 2
  • 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://docs.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

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 Hoffman
  • 805
  • 5
  • 13
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
  • 730
  • 7
  • 20