I have created VIEWS and PartialVIEWS, but so far, i have seen that VIEWS, get rendered/outputted into the RenderBody() section, which is set in the _Layout.cshtml
Hence, if i have in my _Layout.cshtml …
<div id="container">
<div id="col1">
<p>Advert1 aliquip</p>
</div>
<div id="col2outer1">
<div id="col2mid1">
@RenderBody()
<br /><b /> <br />
</div>
<div id="col2side1">
<p>Advert2 </p>
</div>
</div>
ALL VIEWS will be called within the @RenderBody() section. This will mean that Advert1 and Advert2 will always be shown on every VIEW called. However when i call a PartialView, this does not happen. The Advert1 and Advert2 does not appear. How can i get around this without manually creating the above in every PartialView.
Thanks Kindly
Naren
If you’re relying on a
_ViewStart.cshtmlto apply your_Layout.cshtmlto your partial, don’t. Try explicitly setting theLayoutin the initial code block.I use nested layouts for a bunch of custom editor templates in my last project, trying to get a
_ViewStart.cshtmlto kick in for that folder just wouldn’t work because_ViewStartis not executed for Partials. As soon as I manually specifiedLayoutdirectly in the partial it was fine.Personally, I was happy with that – it was only a minor annoyance.
So, as an example:
(In
~/Views/Shared/_PartialLayout.cshtml)And then an example partial is as follows:
(Note you have to explicitly set the layout, because
_ViewStartis not processed for partials)At runtime – this partial will actually render:
Which I believe is what you want to achieve.
Note that the actual location of the partial views’ layout is not important, you can put it in a shared folder if you want, or you can put it in a particular controller’s views folder – so long as you then address it correctly in the partial view’s assignment of the
Layoutmember, it’ll be fine.The answer on this other SO: Correct way to use _viewstart.cshtml and partial Razor views?, which actually makes reference to an earlier bug in Razor, too, exploits the fact that PartialViewResults don’t execute ViewStart.