简体   繁体   English

PartialView不会在_Layout.cshtml RenderBody()部分中输出

[英]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 我创建了VIEWS和PartialVIEWS,但到目前为止,我已经看到了VIEWS,被渲染/输出到RenderBody()部分,该部分在_Layout.cshtml中设置

Hence, if i have in my _Layout.cshtml ... 因此,如果我有_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. 所有视图都将在@RenderBody()部分中调用。 This will mean that Advert1 and Advert2 will always be shown on every VIEW called. 这意味着Advert1和Advert2将始终显示在每个被调用的VIEW上。 However when i call a PartialView, this does not happen. 但是,当我调用PartialView时,这不会发生。 The Advert1 and Advert2 does not appear. Advert1和Advert2不会出现。 How can i get around this without manually creating the above in every PartialView. 如何在不在每个PartialView中手动创建上述内容的情况下解决这个问题。

Thanks Kindly Naren 非常感谢Naren

If I understand right: - your RenderBody Views are non-partial but - your Adv1,2 are partial views? 如果我理解正确: - 您的RenderBody视图是非部分的,但是 - 您的Adv1,2是部分视图?

If so - it should work if you call @Html.RenderPartial("adv1") in your div containers. 如果是这样 - 如果你在div容器中调用@ Html.RenderPartial(“adv1”),它应该可以工作。

If you're relying on a _ViewStart.cshtml to apply your _Layout.cshtml to your partial, don't. 如果您依靠_ViewStart.cshtml_Layout.cshtml应用于部分,请不要。 Try explicitly setting the Layout in the initial code block. 尝试在初始代码块中明确设置Layout

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. 我使用嵌套布局在我的最后一个项目了一堆的自定义编辑模板,试图让_ViewStart.cshtml踢在该文件夹,因为仅仅是行不通的_ViewStart不为局部模板执行。 As soon as I manually specified Layout directly in the partial it was fine. 只要我在部分手动指定Layout就可以了。

Personally, I was happy with that - it was only a minor annoyance. 就个人而言,我很满意 - 这只是一个小麻烦。

So, as an example: 所以,作为一个例子:

(In ~/Views/Shared/_PartialLayout.cshtml ) (在~/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) (注意,您必须显式设置布局,因为_ViewStart不会为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. 请注意,部分视图布局的实际位置并不重要,您可以根据需要将其放在共享文件夹中,也可以将其放在特定控制器的视图文件夹中 - 只要您在部分视图中正确地解决它查看Layout成员的分配,一切都会好的。

The answer on this other SO: Correct way to use _viewstart.cshtml and partial Razor views? 关于这个问题的答案SO: 使用_viewstart.cshtml和部分Razor视图的正确方法是什么? , which actually makes reference to an earlier bug in Razor, too, exploits the fact that PartialViewResults don't execute ViewStart. 实际上也引用了Razor中的早期错误,利用了PartialViewResults不执行ViewStart的事实。

If i have understood your question correctly, using asp.net mvc "sections" could be a solution for your situation. 如果我已正确理解您的问题,使用asp.net mvc“sections”可能是您的情况的解决方案。

定义部分

通话部分

What are you returnung in your Controller class for the View? 您在Controller类中为View返回了什么? Are you returning View or PartialView(m)? 您是否返回View或PartialView(m)? If you return View(m) and render as a Partial that might lead to some strange stuff if I am remembering right.. 如果您返回View(m)并渲染为Partial,如果我记得正确则可能导致一些奇怪的东西..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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