Naren Gokal
Naren Gokal

Reputation: 117

PartialView does not output within the _Layout.cshtml RenderBody() section

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

Upvotes: 1

Views: 5049

Answers (4)

Andras Zoltan
Andras Zoltan

Reputation: 42363

If you're relying on a _ViewStart.cshtml to apply your _Layout.cshtml to your partial, don't. Try explicitly setting the Layout in the initial code block.

I use nested layouts for a bunch of custom editor templates in my last project, trying to get a _ViewStart.cshtml to kick in for that folder just wouldn't work because _ViewStart is not executed for Partials. As soon as I manually specified Layout directly 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)

<div class="partialContainer">
@RenderBody()
</div>

And then an example partial is as follows:

{
  Layout = "~/Views/Shared/_PartialLayout.cshtml";
}
<p>Hello World!</p>

(Note you have to explicitly set the layout, because _ViewStart is not processed for partials)

At runtime - this partial will actually render:

<div class="partialContainer">
<p>Hello World!</p>
</div>

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 Layout member, 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.

Upvotes: 1

Efe Kaptan
Efe Kaptan

Reputation: 465

If i have understood your question correctly, using asp.net mvc "sections" could be a solution for your situation.

define section

call section

Upvotes: 1

Obiwan007
Obiwan007

Reputation: 646

What are you returnung in your Controller class for the View? Are you returning View or PartialView(m)? If you return View(m) and render as a Partial that might lead to some strange stuff if I am remembering right..

Upvotes: 0

Obiwan007
Obiwan007

Reputation: 646

If I understand right: - your RenderBody Views are non-partial but - your Adv1,2 are partial views?

If so - it should work if you call @Html.RenderPartial("adv1") in your div containers.

Upvotes: 1

Related Questions