I've recently upgraded a project from MVC 1 to MVC 3 and now I'm trying out Razor.
In one View, I have a foreach code block, but the nested if statement does not seem to want the @ in front of it.
My original code was:
@foreach(var r in Model.Results)
{
string css = r.Result.Count() > 0 ? "fail" : "pass";
<p class="@css"><strong>@r.Description</strong></p>
@if(r.Result.Count() > 0)
{
<p>Count: @r.Result.Count()</p>
<table>
<thead>
<tr>
<th>ID</th><th>Title</th><th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var e in r.Result) {
<tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
}
</tbody>
</table>
}
}
I'll get a runtime error with @if that says: Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@".
If I remove the @ the code runs fine. I expected to need the @ because of the HTML immediately preceding it. What confuses me more is that I do need the @ before the nested foreach. What are the rules in play here?
Multi-statement Code blockYou can write multiple lines of server-side code enclosed in braces @{ ... } . Each line must ends with a semicolon the same as C#.
Code block is used to enclose C# code statements. It starts with @ (at) character and is enclosed by {} (curly braces).
Razor code blocks Use this approach to render HTML that isn't surrounded by an HTML tag. Without an HTML or Razor tag, a Razor runtime error occurs. The <text> tag is useful to control whitespace when rendering content: Only the content between the <text> tag is rendered.
It is common in most algorithms to have nested code blocks. A simple example would be a program which calculates the sum of all values from 0 to n, where the user enters values for n until a -1 if entered.
Within any parentheses in razor it expects a matching start and end end tag. Thats how the parser works.
So far example the following is valid:
@for (var i = 0; i < 10; i++) {
<p>
@i.ToString()
</p>
}
And this is not:
@for (var i = 0; i < 10; i++) {
<p>
@i.ToString()
</p>
@if (i == 2) {
<p>2</p>
}
}
To get around this you can place it within a <text>
block like:
@for (var i = 0; i < 10; i++) {
<text>
<p>
@i.ToString()
</p>
@if (i == 2) {
<p>2</p>
}
</text>
}
So in your case it would become:
@foreach(var r in Model.Results)
{
@string css = r.Result.Count() > 0 ? "fail" : "pass";
<text>
<p class="@css"><strong>@r.Description</strong></p>
@if(r.Result.Count() > 0)
{
<p>Count: @r.Result.Count()</p>
<table>
<thead>
<tr>
<th>ID</th><th>Title</th><th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var e in r.Result) {
<tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
}
</tbody>
</table>
}
</text>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With