Using a razor code block

I am just starting with web pages and I cannot understand why the code block is not working. I need to conditionally display a WebGrid. Before starting, I have the following:

<div> @grid.GetHtml() </div> 

Now I am trying to insert an if statement:

 <div> @if (condition) { grid.GetHtml(); } </div> 

This results in no html grid display. In fact, even without an if statement and only with a code block, I don't get html:

 <div> @{ grid.GetHtml(); } </div> 

Unfortunately, I am running WebMatrix, so I cannot debug, but it seems that when I use blocks of code, the instructions are not executed.

+4
source share
1 answer

For single line statements, you should use the @ sign instead of putting it in a block of code. If you still put it in a block of code (you need some cases), it will not display your grid just by calling grid.GetHtml(); That's why the @ sign is used, it means that you want to write some value, but you should use it only if you write HTML code. When using a code block, you must tell the server that this value should be written out, for example:

 @{ this.Write(grid.GetHtml()); } 
+3
source

All Articles