4

I can not seem to get this to work.

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

that doesn't work because it says "once inside code you dont' need @ blah blah blah"

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

how can I only show that section conditionally?

Jim G.
  • 15,141
  • 22
  • 103
  • 166
Christopher Johnson
  • 2,629
  • 7
  • 39
  • 70

3 Answers3

6

Depending if your Layout has an alternative for undefined sections or not, you could just reverse the @if and @section

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

If you just want to leave out the nav, this should be fine, but it won't work if you are using IsSectionDefined("Navigation") in your layout, as it will always return true.

Matt Tew
  • 1,581
  • 1
  • 9
  • 15
  • well the problem is, some of my templates won't have the navigation section, therefore the if, I'll will have to use IsSectionDefined in my _layout page. this should work...thanks much – Christopher Johnson Oct 17 '11 at 02:43
  • If you really want to put the if before the section because you are using IsSectionDefined you can look this link: http://stackoverflow.com/questions/5982636/razor-syntax-to-render-section-based-on-condition – plauriola Nov 28 '12 at 14:41
4

The idea:

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

    }
    </text>
}

Here is the code:

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

Happy coding!

Nawaz
  • 124
  • 4
1

The

@section Name {
}

construct is used for defining a section. To render a section you use the RenderSection() method.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • The way I read the question, the OP wishes to define the section on the page (rather than in the layout, as you suppose) conditionally. – Matt Tew Oct 17 '11 at 02:21