November Happy Hour will be moved to Thursday December 5th.

Bug with PropertyFor and BlockData

Vote:
 

Hi

I'm having issues rendering blocks that are not casted directly to block type when they are single elements, but it works when they are list:

ViewComponent:
                var element = _contentLoader.Get<IContent>(myBlockDataContentLink);
                model.MyIContentList.Add(element);
                model.MyIContent = element;
model:
    public IContent MyIContent { get; set; }
    public IList<IContent> MyIContentList { get; set; }
View:
@Html.PropertyFor(m => m.MyIContent)  <--this will not render
@Html.PropertyFor(m => m.MyIContentList) <--this will render
#295603
Jan 30, 2023 14:12
Phong Bui - Oct 27, 2023 4:37
You can create new DisplayTemplates\IConntent.cshtml and call RenderContentData in there. It works on my case
Vote:
 

You must resolve the single block element as its original type. List rendering will look up the content type to fetch the correct template, etc., while the standalone implementation won't.

e.g.

var element = _contentLoader.Get<MyBlock>(myBlockDataContentLink);

I do have to add that this is a pretty strange approach to block rendering and modelling your content. Also, I cannot tell whether Razor Pages is able to render blocks this way.

#295604
Jan 30, 2023 16:24
Vote:
 

I have a field:

    [AllowedTypes(typeof(My1BlockType), typeof(My2BlockType), typeof(My3BlockType))]
    public virtual ContentArea MyBlocks{ get; set; }

I can load it as IContent and get correct value.

If i have to use "Get<SpecificType>" then Allowed types attribute doesn't have much point as i cannot generically render it anyway.

I treat it as a bug explicitly because if i render it as IContent it will fail, but if i render it as List<IContent> it will work correctly.

#295651
Jan 31, 2023 10:44
Vote:
 

Since you have a content area that's the one you should render, not the block

@Html.PropertyFor(x => x.MyBlocks)

It's not a bug.

#295654
Jan 31, 2023 13:41
Vote:
 

While I agree you often will want to use content areas, I've also had view models for a single content property.

I think this may be the same issue I've run into during our CMS 12 upgrade.
There is no default display template for IContent or IContentData, but there is one for ContentData.

Try adding [UIHint("ContentData")] to your view model properties.

Here's my thread when I was encountering an issue:
https://world.optimizely.com/forum/developer-forum/cms-12/thread-container/2022/7/displayfor-on-icontentdata/

#295668
Edited, Jan 31, 2023 15:28
Vote:
 

you cannot render IContent but you can render List<Icontent> and it is not a bug, it's a feature. Todd Howards would be proud.

Why i'm doing it like that:

<div class="hero">propertyFor(myFirstItem)</div>

PropertyFor(otherItems)

#295889
Feb 03, 2023 12:18
Eric Herlitz - Feb 04, 2023 22:34
Resolving content types is expensive, if you want to use your approach I assume it would be possible to write your custom renderer (DisplayTemplate) that will resolve all content types for you.
Mateusz - Feb 06, 2023 7:52
That's probably true, however arguing that this situation is not a bug or at least inconsistent behaviour really shows quality of this support forum, and fact that i would need to consider writing custom rederers to dynamically add css class to element in content list shows elasticity of the product.
Mateusz - Feb 06, 2023 7:52
That's probably true, however arguing that this situation is not a bug or at least inconsistent behaviour really shows quality of this support forum, and fact that i would need to consider writing custom rederers to dynamically add css class to element in content list shows elasticity of the product.
Mateusz - Feb 06, 2023 7:53
edit: if i press submit 2 times i will post message 2 times
wel... nothing more to add
Eric Herlitz - Feb 07, 2023 14:02
It's not inconsistent behavior since List IContent and IContent isn't the same thing. Use the HTML-helpers as illustrated and it will work. Also, read the manual.
* 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.