Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to rendering using ItemRendering in Sitecore

I have a rendering that calls its datasources children. Each child item has a rendering attached in the renderings field. I am calling

@Html.Sitecore().ItemRendering(item)

Which works.

However I want to pass some parameters to the child's rendering, so I tried the following code;

@Html.Sitecore().ItemRendering(item, new { Parameters = "active=1" })

But the parameters do not get passed to the child rendering when I call @Html.Sitecore().CurrentRendering.Parameters["active"]

So I tried @Html.Sitecore().ItemRendering(item, new { Active = 1 }). I called it again in the child rendering and still no luck.

Is there a way to pass parameters to the child using @Html.Sitecore().ItemRendering()

like image 835
Carl Thomas Avatar asked Jun 16 '15 11:06

Carl Thomas


People also ask

Where does Sitecore store the rendering parameter?

Rendering parameter templates are managed in the same module as the code that uses them, either in the feature module where the rendering and controller logic resides or in a foundation level module.


2 Answers

The ItemRendering method does not seem to handle properties correctly (or as one would expect!).

A work-around is to use @Html.Sitecore().Rendering() instead. You can use this in the same way as the ItemRendering method with the work-around below. Note that you should be setting the "Renderers" field of the datasource item (or its template standard values) rather than the "Renderings" field as you mentioned:

@Html.Sitecore().Rendering(item["__Renderers"], new {Datasource = item.ID, Message = "Hello World"})

In the child rendering, use the Properties property, not Parameters:

@Html.Sitecore().CurrentRendering.Properties["Message"]
like image 85
Matthew Dresser Avatar answered Jan 01 '23 12:01

Matthew Dresser


var viewData = new ViewDataDictionary();
viewData["active"] = "1";
Html.Sitecore().ItemRendering(ItemToRender, viewData);

In the rendering for the Item, you can access the viewdata like this:

(ViewData["active"] != null && int.Parse(ViewData["active"].ToString()) == 1)
like image 39
Jan Bühler Avatar answered Jan 01 '23 12:01

Jan Bühler