Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Pass arguments down to blocks?

Vote:
 

I have a block containing a content area. I wish to pass on arguments to the blocks inside this content area from the parent block. Is there any way to do this? 

Another posibility is to get access to the parent block properties from the blocks in the content area. I've seen people talk about accessing ContentPropertiesStack, but this seems a bit tricky, and I haven't really figured this out.

Has anyone done something similar?

#87997
Jun 27, 2014 10:48
Vote:
 

Not sure exactly how your setup is, but is it possible to add arguments as RenderSettings?

#88000
Jun 27, 2014 11:58
Vote:
 

Forgot to mention that this is MVC. Isn't RenderSettings a WebForms specific thing?

I've tried to pass on arguments with

@Html.PropertyFor(x => x.Area, new { CustomSomething = "test" })

the retrive it with

@ViewData["CustomSomething"]

without any luck. I guess this is only available in the ContentArea display template.

#88002
Edited, Jun 27, 2014 12:33
Vote:
 

I have done exactly that to pass down data from Containing ContentArea rendering to blocks

@Html.PropertyFor(model => model.MyContentArea, new { CustomSomething = "something" })
var something = blockController.ControllerContext.ParentActionViewContext.ViewData["CustomSomething"] != null ? blockController.ControllerContext.ParentActionViewContext.ViewData["CustomSomething"] : null;

Not to confuse the matter but I actually use it as a short cut to providing different rendering for the block types.

Rather than implementing new controllers which render when a specific tag is set, I pass a RenderTag down to the blocks using this method.

@Html.PropertyFor(model => model.MyContentArea, new { RenderTag= "specificRenderTag" })

Then in my block controllers I implement as follows:

public override ActionResult Index(MyBlock currentBlock)
        {
            string viewName = this.TryGetRenderTagView();

            return this.PartialView(viewName, currentBlock);
        }

TryGetRenderTagView() will return the default "Index" OR if the RenderTag is found in the ParentActionViewContext AND it can find a view with same name as the RenderTag, then it will return the RenderTag.

public static string TryGetRenderTagView<T>(this EPiServer.Web.Mvc.BlockController<T> blockController) where T : BlockData
        {
            string retval = "Index";
            var parentRenderTag = blockController.TryGetParentRenderTag();
            if (!string.IsNullOrWhiteSpace(parentRenderTag))
            {
                var viewResult = new PartialViewResult().ViewEngineCollection.FindPartialView(blockController.ControllerContext, parentRenderTag);
                if (viewResult != null && viewResult.View != null)
                {
                    retval = parentRenderTag;
                }
            }

            return retval;
        }

public static string TryGetParentRenderTag<T>(this EPiServer.Web.Mvc.BlockController<T> blockController) where T : BlockData
        {
            return blockController.ControllerContext.ParentActionViewContext.ViewData[MagicStrings.ViewDataKeys.RenderTag] != null ? blockController.ControllerContext.ParentActionViewContext.ViewData[MagicStrings.ViewDataKeys.RenderTag].ToString() : null;
        }
#88020
Edited, Jun 27, 2014 16:03
Vote:
 

Looks like this will do the trick. Thanks!

#88037
Jun 30, 2014 9:42
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.