1

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

Naren Gokal
  • 117
  • 8
  • 21

4 Answers4

1

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.

Obiwan007
  • 646
  • 1
  • 8
  • 20
  • Hi, Sorry, I dont quite understand. The above code i have pasted is within the _Layout.cshtml. Every page outputted that is a VIEW, contains everything above (all the divs), and the resultant output of the VIEW, for example, if there is a WEBGRID, is within the @RenderBody() section. But with the PartialVIEW, i dont get the
    that is is the _Layout.cshtml like how i normally do for all VIEWs()
    – Naren Gokal Mar 19 '12 at 11:15
  • +1 Because having gone to-and-fro a couple of times the question is actually 'How to output some content on every page`. – Andras Zoltan Mar 19 '12 at 12:02
  • Yes, that is correct. For at the moment, im having to add the same code as in _Layout.cshtml (the body section) to all my PartialViews – Naren Gokal Mar 19 '12 at 12:10
1

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.

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • Hi, Thanks for the reply. so what u saying is that i should place the same
    code from _Layout.cshtml in my PartialViews ?
    – Naren Gokal Mar 19 '12 at 11:22
  • Hi, I have added this at the top of the PartialView : @{ Layout = "~/Views/Shared/_Layout.cshtml"; } But the format goes all off. My menus appear twice, and the table that gets displayed from the PartialView appears not inline with the sides of the columns defined in the _Layout.cshtml. Any other ideas ? – Naren Gokal Mar 19 '12 at 11:45
  • Hi, Im using MVC3, and the _Layout.cshtml is currently sitting in the Shared Folder. Every VIEW uses this page as its 'master page'. Sorry, im not very clued up on WebForms and just started using MVC3 for 2 months. – Naren Gokal Mar 19 '12 at 11:56
  • Hi, Sorry, there is a misunderstanding. I need to show my _layout.cshtml in every PartialView. The _Layout.cshtml does show in every VIEW(), but not in PartialView(). I need to know how to get the _Layout in every PartialView just like how it appears in a VIEW() – Naren Gokal Mar 19 '12 at 12:25
  • Hi, I have replied to that solution where you said i should add the _Layout.cshtml at the beginning of the PartiaView. What hapens is that when i do this : @{ Layout = "~/Views/Shared/_Layout.cshtml"; } But the format goes all off. My menus appear twice, and the table that gets displayed from the PartialView appears not inline with the sides of the columns defined in the _Layout.cshtml. Any other ideas ? – Naren Gokal Mar 19 '12 at 13:37
  • So i will need to create a new _Layout.cshtml for PartialViews then? – Naren Gokal Mar 20 '12 at 07:57
  • @NarenGokal - have updated my answer with a clearer example. I've also got rid of my other comments, because there were too many there – Andras Zoltan Mar 20 '12 at 08:09
  • Excellent Andras, Thank you, This is what i wanted. I will give it a shot and let you know. Thanks again – Naren Gokal Mar 20 '12 at 09:23
  • Excellent Andras, Thank you, This is what i wanted. I will give it a shot and let you know. Thanks again. Ok, I have made the change. I have created a _PartialLayout.cshtml, similiar to the _Layout.cshtml. I have a div that contains, a left section(for ads) and middle section(for the actual data of the website) and a right side section(for ads).When rendering,everything moves to the right. I have tried removing,changing size of tables,etc..but still. I dont know why.However i am seeing what i want, but the format is not correct.Everything just shifts to the right. Did u get this doing yours ? – Naren Gokal Mar 20 '12 at 10:05
  • Hi, I have resolved it. PartialViews and VIEWS() render differently. I has to change the margin for the partialView css style. Thanks Andras for your help on all of this. – Naren Gokal Mar 20 '12 at 10:12
1

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

define section

call section

Community
  • 1
  • 1
Efe Kaptan
  • 465
  • 2
  • 11
  • Thanks Efe, I have actually changed my entire website to do what you have done above. It was helpful for me, However, I have realised that PartialViews do not look at the _Layout.cshtml, because i have tried the above with PartialViews, and the page is not correct. The sidebar comes first, then below the sideBar is the Body and beloe the body is my right sideBar. – Naren Gokal Mar 20 '12 at 07:56
0

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

Obiwan007
  • 646
  • 1
  • 8
  • 20
  • Ok, what im doing is, the controller is calling a VIEW(). This VIEW(), is making an Ajax call to the Controller[HttpPost] which calls a PartialView() like this: return PartialView("_MyProjectsDetail", projlist). Do you think thats the problem ? – Naren Gokal Mar 20 '12 at 08:00