5

There is need to load a controller rendering from the Razor view.
I'm doing it this way:

@Html.Sitecore().ControllerRendering("Captcha", "GoogleReCaptcha", new { RedisCacheKeyPrefix = "scans_", GoogleReCaptchaValidationMessage = "Please fill up the captcha" })

Here Captcha is the controller name, GoogleReCaptcha is the action name.

RedisCacheKeyPrefix & GoogleReCaptchaValidationMessage are the rendering parameters, i.e the exact field names of the rendering parameter template.

I'm assigning the default values here.

In the code, reading the values:

string prefix= RenderingContext.Current.Rendering.Parameters["RedisCacheKeyPrefix"]  
string message= RenderingContext.Current.Rendering.Parameters["GoogleReCaptchaValidationMessage"]

But they are always null.

What is the correct way to pass the rendering parameters in this case.

2 Answers 2

6

The naming of the 3rd parameter of @Html.Sitecore().ControllerRendering method is a bit unfortunate. It's called parameters, while in my opinion it should be called properties.

The reason for that is one can access what is passed via this parameter using

RenderingContext.Current.Rendering.Properties[...]

So in your example:

@Html.Sitecore().ControllerRendering("Captcha", "GoogleReCaptcha", new { RedisCacheKeyPrefix = "scans_", GoogleReCaptchaValidationMessage = "Please fill up the captcha" })

can be accessed using

string prefix = RenderingContext.Current.Rendering.Properties["RedisCacheKeyPrefix"]  
string message = RenderingContext.Current.Rendering.Properties["GoogleReCaptchaValidationMessage"]
0

I suggest that you create a setting item somewhere in Sitecore that contain's the two properties / fields, and then set the setting item as a DataSource:

@Html.Sitecore().ControllerRendering("Captcha", "GoogleReCaptcha", new
{
    DataSource = "{sitecore-id-of-setting-item}"
})

Something like this. Then your content is editable and you can request the DataSource item (with the two fields) in your controller as usual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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