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

Access/Display contentid in the view of a model?

Vote:
 

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?

#255952
Jun 02, 2021 9:35
Vote:
 

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.

#255953
Edited, Jun 02, 2021 10:59
Vote:
 

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

#255958
Jun 02, 2021 13:46
Vote:
 

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.

#255959
Edited, Jun 02, 2021 13:48
Vote:
 

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

#255960
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.