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()
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.
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"]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With