Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're relying on a <code>_ViewStart.cshtml</code> to apply your <code>_Layout.cshtml</code> to your partial, don't. Try explicitly setting the <code>Layout</code> in the initial code block.</p> <p>I use nested layouts for a bunch of custom editor templates in my last project, trying to get a <code>_ViewStart.cshtml</code> to kick in for that folder just wouldn't work because <code>_ViewStart</code> is not executed for Partials. As soon as I manually specified <code>Layout</code> directly in the partial it was fine.</p> <p>Personally, I was happy with that - it was only a minor annoyance.</p> <p>So, as an example:</p> <p>(In <code>~/Views/Shared/_PartialLayout.cshtml</code>)</p> <pre><code>&lt;div class="partialContainer"&gt; @RenderBody() &lt;/div&gt; </code></pre> <p>And then an example partial is as follows:</p> <pre><code>{ Layout = "~/Views/Shared/_PartialLayout.cshtml"; } &lt;p&gt;Hello World!&lt;/p&gt; </code></pre> <p>(Note you have to <em>explicitly</em> set the layout, because <code>_ViewStart</code> is not processed for partials)</p> <p>At runtime - this partial will actually render:</p> <pre><code>&lt;div class="partialContainer"&gt; &lt;p&gt;Hello World!&lt;/p&gt; &lt;/div&gt; </code></pre> <p>Which I believe is what you want to achieve.</p> <p>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 <code>Layout</code> member, it'll be fine.</p> <p>The answer on this other SO: <a href="https://stackoverflow.com/questions/4081811/correct-way-to-use-viewstart-cshtml-and-partial-razor-views">Correct way to use _viewstart.cshtml and partial Razor views?</a>, which actually makes reference to an earlier bug in Razor, too, exploits the fact that PartialViewResults don't execute ViewStart.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload