Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderSection() issue in _ViewStart.cshtml

I have following code snipped in _Layout.cshtml

<div id="sub-navig-container">
    @RenderSection("subNavig")
</div>

<div id="text-content">
    @RenderBody()
</div>

when i add in my view

@section subNavig
{
    //some code
}

it is work good, but when I write this in _ViewStart I have an error: The name 'DefineSection' does not exist in the current context enter image description here

Please explain why, and if possible tell how can I fixing this issue

like image 638
Artur Keyan Avatar asked Nov 16 '11 06:11

Artur Keyan


1 Answers

_ViewStart is a special view which derives from ViewStartPage instead of WebViewPage which other views derive from. And the ViewStartPage class doesn't have a DefineSection method. So you cannot define sections in this file. You could provide default contents to this section in the layout directly:

<div id="sub-navig-container">
    @if (!IsSectionDefined("subNavig"))
    {
        // some default code
    }
    else
    {
        // render the code that was overridden in the child view
        @RenderSection("subNavig")
    }
</div>
like image 136
Darin Dimitrov Avatar answered Oct 07 '22 22:10

Darin Dimitrov