1

As you can see below my MVC site contain _ViewStart.cshtml file in the root of my Views folder that use the _Layout.cshtml from Shared folder for sharing the layout of Header and Footer. But the Main Content is different for each page.

enter image description here

But now I have same Main Content layout for my Category folder pages. So my question is, how can I use another updated _Layout and _ViewStart files for Category folder pages that also use the Header and Footer by my _Layout.cshtml and _ViewStart.cshtml files or I need to specify the same Section for each page in Category folder? I want to use something like _ViewStart.cshtml file in the Root of Category folder that can share Main Content layout for Category pages.

Perhaps it seems a bit confusing, but I hope you can understand what I'm trying to ask.

Anonymous
  • 10,002
  • 3
  • 23
  • 39
  • Can't you use another layout which you only use for the category pages? Within this layout you can include the main layout, this way you make use of both layouts. – DerDee Mar 04 '14 at 08:26
  • @DerDee - What you mean by "Within this layout"? – Anonymous Mar 04 '14 at 08:27

1 Answers1

1

You make a _CategoryLayout.cshtml within the category folder and put the following in the category page(s):

@{ Layout = "~/Views/Category/_CategoryLayout.cshtml"; }

Then you can use this layout to add footers and headers to your page, in turn this layout page (_CategoryLayout) can have its own layout. So you can add the following code in that layout:

@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

This way it makes use of both layouts. You can ofcourse get more out of it by applying the right Sections, don't forget to fill a section in the page itself you need to define the section in every layer of layout till you display it. So if you for example have a CSS section which you want to use in the category page, you also would need to define it in the categorylayout.

Structure of nested layouts

DerDee
  • 392
  • 2
  • 10
  • Reading your question again makes me wonder if I weren't a bit too far already, seems like you only need a new layout for the category pages. This is simply done by : @{ Layout = "~/Views/Category/_CategoryLayout.cshtml"; } . To skip the MainLayout and you need set the Layout variable in CategoryLayout to Null: @{ Layout = null; } – DerDee Mar 04 '14 at 08:40
  • Did this answer your question? if so could you mark it as answer, can you explain what you would like to know? Thank you – DerDee Mar 04 '14 at 09:04
  • Don't you think it's better to have more answers instead of few answers. It's just human nature - "Finding more accurate answers to their Questions" and I will accept your answer later if not, Sorry for being greedy. :) – Anonymous Mar 04 '14 at 09:12
  • No need of being sorry, it's being greedy in science, which is the only good greedy if you ask me. Im pretty sure this is the best practice when it comes to layouts, but I let new answers surprise and teach me. (just dont forget me ;)) – DerDee Mar 04 '14 at 09:30
  • I have one question, as I already mentioned about _ViewStart.cshtml file, can't I use it with @{ Layout = "~/Views/Category/_CategoryLayout.cshtml"; } by using both layouts as you said in your answer. Can't I use a different _ViewStart for share the content layout only for category pages. – Anonymous Mar 04 '14 at 09:31
  • I've been looking up the purpose of _ViewStart since I never use it myself, it seems like it's overwritable to be put inside the folder allongside the category page as descriped in here: http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file This is MVC3 but probably still working for MVC5 since the structure of these View folders are still the same. – DerDee Mar 04 '14 at 09:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48942/discussion-between-derdee-and-mary-melody) – DerDee Mar 04 '14 at 09:45