Putting @section inside @ if.net mvc 3

I can't get this to work.

@if (isReal) { @section Navigation{ @{Html.RenderPartial("NavigationForInput");} } } 

which doesn’t work because it says "once inside the code you don’t need" @blah blah blah "

but when I remove @ from before the section, it wants to use the section as a type variable.

How can I only show this section conditionally?

+4
source share
3 answers

Depending on whether your layout has an alternative for undefined sections or not, you can simply cancel @if and @section

 @section Navigation{ { @if (isReal) @{Html.RenderPartial("NavigationForInput");} } } 

If you just want to leave the navigation, this should be fine, but it will not work if you use IsSectionDefined ("Navigation") in your layout, as it will always return true.

+5
source

Idea:

 @if (Condition) { <text> @section SectionName { } </text> } 

Here is the code:

 @if (isReal) { <text> @section Navigation{ @{Html.RenderPartial("NavigationForInput");} } </text> } 

Happy coding!

+4
source

 @section Name { } 
Design

used to determine the section. To make a section, you use the RenderSection() method.

+1
source

All Articles