Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting a @section inside an @if .net mvc 3

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?

like image 900
Christopher Johnson Avatar asked Dec 27 '22 12:12

Christopher Johnson


2 Answers

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.

like image 62
Matt Tew Avatar answered Jan 05 '23 15:01

Matt Tew


The idea:

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

    }
    </text>
}

Here is the code:

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

Happy coding!

like image 30
Nawaz Avatar answered Jan 05 '23 15:01

Nawaz