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

Try our conversational search powered by Generative AI!

Call method from content reference

Crb
Crb
Vote:
 

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)
{
var Block = ServiceLocator.Current.GetInstance().Get(item.ContentLink);

//here i need to call the method

@Html.PropertyFor(m =>Block)

}

#178387
May 10, 2017 15:26
Vote:
 

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.

#178388
Edited, May 10, 2017 16:17
Vote:
 

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

#178389
May 10, 2017 18:18
Crb
Vote:
 

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.

#178446
May 11, 2017 8:54
Vote:
 

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

#178451
May 11, 2017 9:44
Vote:
 

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.

#178474
May 11, 2017 14:22
Vote:
 

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.





                        
#178726
May 19, 2017 9:15
Crb
Vote:
 

I actually managed to pass this issue. Seems like casting to the block base type do the trick and is working great.

#178729
May 19, 2017 9:55
* 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.