0

Trying to create code section within an If condition throws error: Unexpected "section" keyword after "@" character. Once inside code, you do not need to prefix constructs like "section" with "@".

Example 1:Create section when needed
@If bIsMobile = True Then
@section PageFooter
    <div data-role="navbar">
        <ul>
            <li><a href="#">Add</a></li>
            <li><a href="#">Edit</a></li>
            <li><a href="#">Delete</a></li>
            <li><a href="#">Search</a></li>
            <li><a href="#">Refresh</a></li>
        </ul>
    </div>
End Section
End If

Obviously we could move the condition inside the section, but this forces processing of empty section blocks:

Example 2:Creates empty section
@section PageFooter
@If bIsMobile = True Then
    <div data-role="navbar">
        <ul>
            <li><a href="#">Add</a></li>
            <li><a href="#">Edit</a></li>
            <li><a href="#">Delete</a></li>
            <li><a href="#">Search</a></li>
            <li><a href="#">Refresh</a></li>
        </ul>
    </div>
End If
End Section

How can we implement Example #1

osstech
  • 33
  • 1
  • 6

2 Answers2

1

@section blocks can only appear in markup context. Thanks to SLacks for this answer.

@If bIsMobile = True Then
<text>
   @section PageFooter
     <div data-role="navbar">
        <ul>
            <li><a href="#">Add</a></li>
            <li><a href="#">Edit</a></li>
            <li><a href="#">Delete</a></li>
            <li><a href="#">Search</a></li>
            <li><a href="#">Refresh</a></li>
        </ul>
     </div>
   End Section
</text>
End If

And check in _Layout page whether section is defined for non mobile pages. Otherwise it will throw an exception stating that the “PageFooter” section wasn’t defined.

<footer>
  @If IsSectionDefined("PageFooter") Then
      RenderSection("PageFooter")
  End If
</footer>
Community
  • 1
  • 1
Min Min
  • 6,188
  • 2
  • 19
  • 17
0

The second example that you gave is the correct one.

As far as I know you can't specify to the razor engine to include a section in a conditional or loop block.

linkerro
  • 5,318
  • 3
  • 25
  • 29