How can I identify data type of BlocklistModel in code?
Is there any way to check the underlying datatype of a blocklist in code?
Let's say I have created a blocklist data type and called it fooBlocklist and another one called barBlocklist and added a property to a document type which uses the fooBlocklist.
When accessing this property in code, I get a BlockListModel, and I can loop through the BlockListItems in the block, but is there any way to determine if the BlockListModel is using the barBlocklist or the fooBlocklist?
Something like this in pseudo:
if (model.myBlockListProperty is BlockListModel<fooBlockList>)
{
//do stuff
}
So far, my best approach has been to check if all blockitems inside the BlockListModel are of a certain type, because I know I only have one blocklist with this specific configuration of blocks, but this approach doesn't work if the blocklist is empty.
but this approach doesn't work if the blocklist is empty.
You could easily add a
if (myBlockList.Any())
To check if there are any items ofcourse ;-)
I've been trying this myself aswell, and I think it's not directly possible like you want it. It's probably also the reason Umbraco provides the demo with rendering the items in the blocklist as seperate components:
How can I identify data type of BlocklistModel in code?
Is there any way to check the underlying datatype of a blocklist in code? Let's say I have created a blocklist data type and called it fooBlocklist and another one called barBlocklist and added a property to a document type which uses the fooBlocklist.
When accessing this property in code, I get a BlockListModel, and I can loop through the BlockListItems in the block, but is there any way to determine if the BlockListModel is using the barBlocklist or the fooBlocklist?
Something like this in pseudo:
So far, my best approach has been to check if all blockitems inside the BlockListModel are of a certain type, because I know I only have one blocklist with this specific configuration of blocks, but this approach doesn't work if the blocklist is empty.
You could easily add a
To check if there are any items ofcourse ;-)
I've been trying this myself aswell, and I think it's not directly possible like you want it. It's probably also the reason Umbraco provides the demo with rendering the items in the blocklist as seperate components:
This way you avoid the issue entirely, and always render a component with the correct model.
However! You could do exactly as Umbraco does but in a switch/case for example:
is working on a reply...