Have you tried casting 'Block' that is an ITextButton to the class/interface that have that method?
Something like this(freehand):
var blockInstance = Block as ICallableBlock; if(blockInstance!=null){ var result = blockInstance.GetFunctionValueFromBlock(); //Use result }
Although your premise is a bit weird.
Normally you just render a contentarea directly with a propertyfor and handle anything else in the controllers/views for the blocks.
Hi @Crb,
This may be a bit more than what you need, but I thought I would share this in case it's helpful. Another option is using a class that inherits from the ContentAreaRenderer class. This class has overrides that allow you to do tasks such as customizing the rendering of each item or adding a custom class or container HTML tag to each item. Here's a link to the class information (constructors, methods, properties). If you do end up using a class that inherits from ContentAreaRenderer, you will need to initialize this via an Initialization Module.
Hope this helps!
-RJ
No, it's not woking.
Actually, i need the entire block and all data inside it. I need the object BlockObject, that will be passed as parameter. I can cast it to the specific block type (var blockInstance = Block as BlockObject) and then use it but since there are several blocks and new blocks may appear, this is not a good solution. I can find the original type but I can't cast the Block variable to a type stored in a variable.
It is not be ideal But you can create a Property with Ignore attribute in block and use that, you can do you custom calculations under that property.
/K
Either create a controller for the specific block types or create your custom content area renderer and call the method in one of the render methods in your custom renderer. Check the Alloy project on how to create a custom content area renderer.
Hi,
I would suggest you to get the content of block in the controller, invoke the method and pass the values from controller to the view and use them while the block is rendered.
The method could be a helper method which should be invoked from the ActionResult.
public List<element> GetContentFromBlock (PageType currentPage)
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
List<element> result = new List<element>();
foreach (var c in currentPage.BlockType.Items)
{
element res = new element();
var contentLinkTemp = contentRepository.Get<BlockType>(c.ContentLink);
//In contentLinkTemp you will be getting a list of objects available inside the block of Type "BlockType" which is available in the page of type PageType.
//Here you can call the desired method for each item and store them in the result list and same can be returned.
}
}
I have used a similar method to get the content of blocks beforehand, and manipulate the data and then passing them on to the view.
Hello guys,
I have a page that contains a content area with multiple (and different) blocks.
In view I render each block using a foreach loop but before rendering I need to call a method from the blocks (that returns something). How can I do that?
This is my code so far:
foreach (var item in Model.FormBlocks.tems)().Get(item.ContentLink);
{
var Block = ServiceLocator.Current.GetInstance
//here i need to call the method
@Html.PropertyFor(m =>Block)
}