I'm having troubles finding out how to set the default content of a section when working with MVC Views.
I have a master template called _Layout.cshtml, where I have a section called @RenderSection("header",false). In most of my templates, I just want the content to be inherited from the master, but in a few I would like to modify it.
Before working with views I would have done it like this.
Setting default content of a section (MVC)
I'm having troubles finding out how to set the default content of a section when working with MVC Views.
I have a master template called _Layout.cshtml, where I have a section called @RenderSection("header",false). In most of my templates, I just want the content to be inherited from the master, but in a few I would like to modify it.
Before working with views I would have done it like this.
Master:
Page template:
How's that achieved when working with Views?
I think this blog post should explain a solution to your problem. The first solution being to check if a child section has been defined...
@if (IsSectionDefined("Footer")) { RenderSection("Footer"); } else { This is the default yo! }
And the second being a custom helper method which accepts some default content...
@RenderSection("Footer", @This is the default!)
Thanks!
I don't get the second part, but the first is working, so I'm happy :)
- E
is working on a reply...