Try our conversational search powered by Generative AI!

How to render block on _layout ?

Vote:
 

Hello, I am developing MVC Episerver Site. I am trying  to find best solution for displaying top banner (image) near logo. I found one solution: create banner block type , create view and add bunner to Home Page (Start Page) class. Then send view model to _Layout (_Root) and display it. 

 Example: 

   _Root.cshtml


        
@if(!Model.Layout.HideHeader) { @Html.Partial("Header", Model) } @RenderBody() @if(!Model.Layout.HideHeader) { Html.RenderPartial("Footer", Model); } @Html.RequiredClientResources("Footer")
@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", false) @RenderSection("scripts", false)

and then in Header.cshtml

 

 But as for me sending view model to _Layout (_Root) view is bad idea. So is it possible for example call Block Action method in _layout (_Root) view like:

    

  @Html.Action("Index","TopBannerBlock")

Any ideas ?

#89896
Aug 28, 2014 11:50
Vote:
 

It should be possible to call an action method for a block in a layout file but you should use @Html.PropertyFor() to use EPiServer CMS built in flow for resolving controller.

Are you aware of the performance problems with child actions?

Could you describe the scenario a little bit more? There are many ways to stich this together so we may give you better advise if you try to explain the problem you want to solve and the issue you see with populating the view model with data needed for the header.

#89991
Aug 31, 2014 15:50
Vote:
 

Hi @Fredrik Haglund ! I am trying to display banner (image) under main menu panel (like a logo, but in the centre). I would like to have possibility to change this image in the future in admin panel. Because now its a static image in Resourses folder of the project. So I would like to find best solution for displaying and administrating this one banner (image). I created block with image, created controller and view. Found example , where such block is a property of startpage and startpage model and in _Header this block is calling @Html.PropertyFor(x=>x.HeaderBanner).

#90103
Sep 02, 2014 10:44
Vote:
 

Hi Oleh,

You can take a look at AlloyTech site, and how they solved this with Logo. You can use the same logic for banners.

Basically you need one inline block for Banner.
You can create it on the start page so that editors can change it.
In PageViewContextFactory, fetch the Banner from the StartPage and update your LayoutModel class.
And then in layout / header you can use @Html.PropertyFor(x => x.Layout.Banner) or something like that

#90170
Sep 03, 2014 12:19
Vote:
 

Hi @Dejan Caric  , thanks for answer, but I have one more question: is there some other way for dispaying such bunners? Is there possibility to display banners without sending LeyoutModel to layout / header ?  

#90178
Sep 03, 2014 12:31
Vote:
 

The reason it has to be handled a little bit diffrent is because it is going to be used on all pages of your site.

The ViewModel is a composite between the current pages data and all data fetched/calculated from start page (and other places) used on all pages in header and footer.

So, you allways need to have some code to get what to show in the header and footer but it can of course be done in other ways.

Another aproach would be to introduce another property on you viewmodel: StartPage (or SettingsPage if you have a separate settings page). Then you can just render properties from that page without any coding.

Note that you do have to tweak stuff to get on-page editing of these properties to work on the start page or settings page.

#90180
Sep 03, 2014 12:45
Vote:
 

Hi Oleh,

Yes, there are other more or less painful ways, but I would go for LayoutModel approach because Render.Action is an "expensive" operation.
If the banner has to be displayed on Start Page only, then you could use RenderSection and a single property on your StartPageViewModel.
Otherwise, use LayoutModel :)

#90182
Sep 03, 2014 12:45
Vote:
 

@Fredrik Haglund  , @Dejan Caric Thanks guys !!!

#90184
Sep 03, 2014 12:47
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.