Pass parameters to rendering using ItemRendering in Sitecore

3.9k views Asked by At

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()

2

There are 2 answers

0
Jan Bühler On
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)
1
Matthew Dresser On

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"]