episerver

Access/Display contentid in the view of a model?


I have created a new block type in episerver, and I am working on editing it's corresponding view. I would like to be able to display the contentid of the block in my html so that I can work some magic using javascript.

How can I get the contentid of the block from within it's view?

I would like to be able to do this:

<div class="something" data-id="@Model.contentid"></div>

Solution

  • You need to cast the block instance to IContent and then use @Model.ContentLink.ID.

    So, something like:

    <div class="something" data-id="@(((IContent)Model).ContentLink.ID)"></div>
    

    Reason for the cast is that the IContent interface is only implemented by the proxy class for your block type, which is why you won't find the ContentLink property on a BlockData instance directly.

    If you're interested in the details: https://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Shared-blocks--IContent/