Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Question about nested code block declarations in Razor

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...