1

I am exploring the use of Sections to create optional content in my site's layout, as explained quite neatly in ScottGu's article here:

http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor

However, what I'm trying to do is add a sidebar to the views related to a specific controller, where the sidebar contains static information relevant to the controller. The data won't be tied to the model - it'll just be some links to external pages, and some static 'helper' text.

I don't want the Sidebar on every page / view - just on some views (in my case, it's the view related to a specific controller).

What I don't want to do is have to declare the same Section and content for each view that needs this. I was trying to find some way of creating a 'shared section' - that I could reference neatly in the views that require it, and pass back to the _Layout when the View is returned by the controller.

Partial Views seemed to be the way forward with this:

Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

But I'm not sure if this is the right way to go about this, or if there's more succinct way to achieve what I'm aiming for.

I have been searching for answers on this, but as ever with this sort of thing, I am 100% sure I am not using the correct terminology in my search attempts for this - so apologies if this is duplicate post, basic stuff, or has already been answered elsewhere.

Any pointers on this - even if it's just to give me some direction on the terms to search on - would be welcome !

I've not provided any example code here, but could possibly knock something up if it helps articulate things.

Cheers

Community
  • 1
  • 1
Clueless99
  • 25
  • 6

2 Answers2

1

It sounds like you need Layout nesting, rather than sections.

You can create a master layout, then have a layout that contains the sidebar, making it's Layout property point to your master layout. Then, you can choose which layout for your content pages to use, simply by specifying Layout = Master.cshtml or Layout = Sidebar.cshtml

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Thanks Erik - that looks to be exactly what I need - a nice, easily maintained approch. – Clueless99 Mar 06 '16 at 15:23
  • For anyone stumbling across this thread, the following article gives a good basic example of Nested Layouts: http://www.mikesdotnetting.com/article/164/nested-layout-pages-with-razor – Clueless99 Mar 06 '16 at 15:24
0

I think the approach outlined in the link you provided is very feasible.

Another alternative, depending on the complexity involved in getting the sidebar in your layout, you could create a separate layout.

SvenAelterman
  • 1,532
  • 1
  • 15
  • 26