简体   繁体   中英

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

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.

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.

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

定义部分

通话部分

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM