Developer forum

Forum » Templates » Declaring/using variable across paragraph templates

Declaring/using variable across paragraph templates

Hans Ravnsfjall
Hans Ravnsfjall


I have started posts about this before, but this is maybe a little bit different.

I want to declare a variable in paragraphTemplateA.cshtml, and then I want to use/show it in paragraphTemplateB.cshtml

I was thinking i could use ViewBag for this, but it doesn´t work.


So far I have this in ParagraphTemplateA.cshtml



And try to use it like this in ParagraphTemplateB.cshtml


<div class="col-xl-4 col-12 folk starvsf @ViewBag.DeildType">


But it doesn´t work.


I have attached both paragraph templates

NB: They are both rendered on the same page

What am I doing wrong?





Nicolai Pedersen
Nicolai Pedersen

Each template has its own rendering instance and hence does not share viewbag - you can use the viewbag in include, masters etc.

But simply use httpcontext.items:

HttpContext.Current.Items["Test"] = "Hello World";


Hans Ravnsfjall
Hans Ravnsfjall

Perfect Nicolai 👍🏻

Works like a charm thank you :)



You must be logged in to post in the forum