Try our conversational search powered by Generative AI!

How to read the block type placed in the ContentArea

Vote:
 

I have a block and ContentArea property like below:

public class QuestionnaireBlock : BlockBase
{
[CultureSpecific]
[Display(Name = "Questions Content Area", GroupName = PropertyGroupNames.Content, Order = 10)]
public virtual ContentArea QuestionsContentArea { get; set; }
}

I place a block inside this QuestionsContentArea property as given below:

[CultureSpecific]
[Display(Name = "Question Text", GroupName = PropertyGroupNames.Content, Order = 10)]
public virtual string QuestionText { get; set; }

[CultureSpecific]
[Display(Name = "Items", GroupName = PropertyGroupNames.Content, Order = 20)]
[EditorDescriptor(EditorDescriptorType = typeof(AnswerListPropertyDescriptor))]
[ListItems(4)]
public virtual IList<QuestionnaireAnswerBlock> Answers { get; set; }
}

how do I get the values of the properties of the block placed inside a contentarea in my model of type QuestionnaireBlock in the controller class

#224068
Jun 10, 2020 13:35
Vote:
 

Hi Tanvi,

You should use the AllowedType attribute to ContentArea.

 [AllowedTypes(
        AllowedTypes = new[] {typeof(CalloutBlock )})]
    public virtual ContentArea MainContentArea { get; set; }

Then you can loop through the items and cast them to appropriate type and access their properties

@foreach (var item in Model.MainContent.FilteredItems.Select(x => x.GetContent()))
{
    if (item is CalloutBlock calloutBlock)
    {
        @Html.PropertyFor(x => calloutBlock.Title)
    }
    else if (item is ParagraphBlock paragraphBlock)
    {
        @Html.PropertyFor(x => paragraphBlock.CopyTop)
    }
}
#224069
Edited, Jun 10, 2020 14:14
Vote:
 

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

foreach (var item in currentContent.QuestionsContentArea.Items)
{
var qnBlockItem = contentLoader.Get<IContentData>(item.ContentLink) as <Your BlockType >;

}

#224106
Jun 11, 2020 8:49
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.