Developer forum

Forum » Development » Get access to code-first item object in paragraph template

Get access to code-first item object in paragraph template

Martijn van Tongeren
Reply

I've created a code-first ItemType and now I'd like to create a paragraph template for it. Now I'd also like to use that object strongly typed in my template.

At the moment I'm inheriting from Dynamicweb.Rendering.ViewModelTemplate<ParagraphViewModel>. I can use methods like Model.Item.GetString to get content from my item, but I'd really like to use the item object itself.

It's possible to use var item = ItemManager.Storage.GetById<ExampleItem>(Model.ItemId); to get my item, but it's probably a bad idea to query the item in my template, especially since it's already available in the ParagraphViewModel. There is the method ToCodeFirstItem in the ItemEntry class, but I don't seem to have access to that method via the ParagraphViewModel.


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

You can use the following method to retrieve the item from cache...

var item = Dynamicweb.Services.Items.GetItem(Model.ItemType, Model.ItemId).ToCodeFirstItem<ExampleItem>();

Best regards,
Morten

 

You must be logged in to post in the forum