Defining a Code Block

We can improve on the @for example by using a code block. Listing 25-21 provides a demonstration.

Listing 25-21. Using a Razor code block

... <table>     <tr><th>Index</th><th>Name</th><th>Age</th><th>Home City</th><th>Event</th></tr>     @{         Registration[] regArray = Model.ToArray();         int itemCount = regArray.Length;     }     @for (int i = 0; i < itemCount; i++) {         <tr>             <td>@(i + 1)</td>             <td>@regArray[i].Name</td>             <td>@regArray[i].Age</td>             <td>@regArray[i].HomeCity</td>             <td>@regArray[i].Competition.Name</td>         </tr>     } </table> ...

We start a code block with the @{ and close it with }. Razor applies the same rules to code blocks ...

Get Applied ASP.NET 4 in Context now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.