4

Method definition:

public virtual HtmlString ItemRendering(Item item, object parameters);

Assumed usage:

@Html.Sitecore().ItemRendering( item, new { ThisShouldWork = true } );

Assumed Consumption:

var testVal = Html.Sitecore().CurrentRendering.Parameters["ThisShouldWork"];

in this case, testVal is null. In fact, there are no parameters at all (zero items, the actual object is not null). According to other SO Answers and blog posts, this isn't so clear cut.

So far, I've seen things on workarounds to mitigate the problem of passing data, but I have yet to see anything on what the heck the object parameters intended usage actually is. What does it do? How are we supposed to use it?

3

The parameters dynamic object on the constructor of an ItemRendering is not related to rendering parameters. If you follow the code through using dotPeek - you will see that it adds those values to a .Properties property.

This is of the type RenderingProperties. That object can be used to store data for the rendering. For example, it is used in the mvc.getModel pipeline in the GetFromProperty processor to store the model:

protected virtual object GetFromPropertyValue(Rendering rendering, GetModelArgs args)
{
    string property = rendering.Properties["Model"];
    if (property.IsWhiteSpaceOrNull())
        return (object) null;
    return args.ModelLocator.GetModel(property, true);
}

To get at those properties from your CurrentRendering object, you should be able to do this:

var testVal = Html.Sitecore().CurrentRendering.Properties["ThisShouldWork"];

Update

Here is the code that sets that property. It is called from the GetRendering method:

protected virtual Rendering GetRendering(
    string renderingType, 
    object parameters, 
    params string[] defaultValues)

In that method it does:

if (parameters != null)
    TypeHelper.GetProperties(parameters).Each<KeyValuePair<string, object>>(
        pair => 
            rendering.Properties[pair.Key] = pair.Value.ValueOrDefault((o => o.ToString())));

This takes the dynamic object and sets the .Properties object on the Rendering.

| improve this answer | |
  • Have you confirmed this? Your explanation makes sense to me, but it does not seem to be quite that simple. The Html.Sitecore().CurrentRendering.Properties on the child rendering has one item in it's collection ("RenderingItemPath"), but nothing related to what I'm attempting to pass in. – chad Nov 7 '17 at 17:54
  • Updated with the code that sets those properties. – Richard Seal Nov 7 '17 at 17:59
0

From the look of it the parameters are passed to the rendering properties - not parameters. So try this:

var testVal = Html.Sitecore().CurrentRendering.Properties["ThisShouldWork"];

I haven't tested this myself, but from the look of the decompiled code this should work.

To answer your question - the decompilation of the Sitecore code shows that the

object parameters

are directly mapped as properties on the rendering. As to why they are mapped to properties instead of parameters I don't know.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.