How to put a @ inside {} in MVC

Asked

Viewed 1,076 times

7

While doing a MVC5 page with Razor, I went to put the following code

   {--conteúdo da @ RenderBody() etc, etc }

Generated an error because the @ is inside keys

ERROR:

Server Error in '/' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: A space or line break was encountered after the "@" character.  Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "@" with no space in between.


Source Error: 


Line 74:         </aside>
Line 75:         <!-- PAINEL PRINCIPAL -->
Line 76: {--conteúdo da @ RenderBody()
Line 77:         <div id="main">     
Line 78:             <div id="ribbon">

Source File: /Views/Home/padroes_layout.cshtml    Line: 76 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009

How to put the arroba inside? and if I want to write @Renderbody() there is no way?

  • 1

    You want to return only the text written 'Renderbody()' or you want to render the page ?

  • Please give some feedback... managed to resolve your doubt?

2 answers

6


Alternative 1

Just type in the duplicate symbol: @@.

This will be turning into just one @ in output.

In short, correlating with your question, to write "@RenderBody()" in the output of an Razor view, just type like this:

@@RenderBody()

Alternative 2

Another option, which may be useful in other cases, is to make Razor render an object, which can be anything, including the string "@RenderBody()":

@("@RenderBody()")
  • 2

    @Downvoter: could you explain the negative vote? What’s wrong?

2

Use of (@) in Razor

"The special character @ starts an expression, a block with a single instruction or several, for example, we can insert a Razor code directly into the HTML markup, using the @ character to start a condition expression or display the contents of a variable." (Alexandre Tadashi Sato, Introduction to ASP.NET Razor. Available <http://msdn.microsoft.com/pt-br/library/gg675215.aspx>, accessed on 30 April 2014)

How to use:

Declaring and using variables

@{
    var texto = "Mostrar um Texto";
    var ativo = false;
}
<p>@texto</p>

With if/Else

@if (ativo){
    <div>Está ativo</div>
} else {
    <div>Não está ativo</div>
}

For as long as

@for(var a = 0; a <= 5; a++) 
{         
   <p>Imprimir número: @a</p> 
}

With While

@{
 var cont = 0;
}

@while (cont < 100){
      <p>@cont</p>
      cont++;
}

On the Question

The Renderbody means that the page will be rendered here, it already has a feature and I don’t see the need to print the text that way, since Razor demonstrates other ways as mentioned above.

Works:

@RenderBody();

@{
   @RenderBody();
}

Doesn’t work

@{ @@RenderBody(); }

Works like a text on the screen by downloading such functionality

{@@RenderBody();}

References

  • 1

    I would also like to know who is voting against ...

  • 1

    Because it’s expensive, the same thing happened in my answer... someone voted negative, without even explaining.

  • I agree with you @Miguelangelo, you’re right, but, this is trivial here was taken from my 2 votes understood ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.