2

In my Views root folder I have _ViewStart.cshtml, which has:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
Page.Title = "Using Root ViewStart!";
}

Then in a nested Views\ProjectCharter folder, I have another _ViewStart.cshtml, which looks like this:

@{
    Layout = "~/Views/Shared/_ProjectLayout.cshtml";
    Page.Title = "Using Nested ViewStart!";
}

(note that both the _Layout.cshtml and the _ProjectLayout.cshtml file are in the same folder, called Views\Shared).

The problem I'm having is that the views in my Views\ProjectCharter folder are NOT using the _ProjectLayout.cshtml Layout...instead they are still using the root _Layout.cshtml (even though they are correctly picking up the "Using Nested ViewStart" title).

What's interesting is that if I change my ActionMethod to return the View using

return View("Create","~/Views/Shared/_ProjectLayout.cshtml",newProjectCharter);

instead of just

return View(newProjectCharter);

then the view does indeed use the _ProjectCharterLayout.cshtml layout. Any idea what I am missing? I don't want to have to change all my ActionMethods to use this more verbose overload.

Shawn de Wet
  • 5,642
  • 6
  • 57
  • 88
  • Just thinking out loud but both Layout pages are well formed and have correct namespaces? – David Diez Mar 27 '12 at 11:14
  • Yup. As indicated, it works when I use the more verbose overload of return View(). If there were a problem with namespacing or well-formedness, using the more verbose overload also would not work. – Shawn de Wet Mar 27 '12 at 17:53
  • Look at [this post](http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file). It seems an issue on MVC3 :/ but this guy has a nice solution – David Diez Mar 28 '12 at 08:32

1 Answers1

0

As I can see from your post you are referring to _ProjectLayout.cshtml being in "

Views\ 'PROJECTCHARTER'

but in the code you are saying that it is in the shared folder:

Layout = "~/Views/ 'SHARED' /_ProjectLayout.cshtml";

Hope that will sort it out

Community
  • 1
  • 1
jacqijvv
  • 870
  • 6
  • 17
  • Nope, sorry for not being clear on this...but both the _Layout.cshtml and the _ProjectLayout.cshtml file are in the same folder, called Views\Shared; I'll edit my post to make this clear. – Shawn de Wet Mar 17 '12 at 08:54
  • In the pages that you use in the ProjectCharter controller cant you just instead of adding a viewstart there just at the top of each .cshtml page add the layout manualy? That is to add `Layout = "~/Views/Shared/_ProjectLayout.cshtml";` to the top of each page – jacqijvv Mar 17 '12 at 08:58
  • Well yes I could, but that requires a change to each view - going against the DRY principle. – Shawn de Wet Mar 17 '12 at 12:12
  • See if this posts helps you `http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file` – jacqijvv Mar 17 '12 at 12:21
  • Yup, I read through that question b4 I posted mine. It's actually from that question where I got the hint to try the overload of the return View() method that I mention in my question. – Shawn de Wet Mar 17 '12 at 14:56