SaaS CMS has officially launched! Learn more now.

Access/Display contentid in the view of a model?


I have created a new block type, 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?

Jun 02, 2021 9:35

The ID is in the ContentReference of the block, if you're using a controller and a view/viewmodel you want to cast the Block in the controller method to IContent e.g.

        public override ActionResult Index(CachedBlock currentBlock)
            var content = (IContent) currentBlock;
            var id = content.ContentLink.ID;
            // Set in viewmodel object

If you're using a controllerless block you can do the same in the View in some inline C# or a C# block at the top of the page.

Edited, Jun 02, 2021 10:59

Would you have a suggestion for an inline C# approach? My block does not have a controller.

Jun 02, 2021 13:46

Yes as I said in my reply on the last line it's the same code just put it in a C# code block at the top of the view and cast the model.

Edited, Jun 02, 2021 13:48

Oh sorry, I misunderstood. Re-reading your commend you do indeed suggest that alternative. Thank you I am going to try that. 

Jun 02, 2021 13:50
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.