Pergunta

I'm just getting started with webpages and I can't quite figure out why a code block doesn't work. I need to conditionally display a WebGrid. Before I started I have this:

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

Now I try to insert an if statement:

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

This results in no grid html displaying. In fact even without the if statement and just with a code block I get no html:

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

Unfortunately i'm running WebMatrix so I can't debug, but it seems whenever I use code blocks the statements don't execute.

Foi útil?

Solução

For one line statements you should use the @ sign instead of putting it in a code block. If you still put it in a code block (there are some cases you need to) it won't display your grid by just calling grid.GetHtml(); This is why the @ sign is used, it means that you want to write out some value, but you should only use this if you are writing in HTML code. When using a code block, you have to tell the server that this value should be written out, like this:

@{
this.Write(grid.GetHtml());
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top