Developer forum

Forum » Templates » Declaring/using variable across paragraph templates

Declaring/using variable across paragraph templates

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

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


@{
 
ViewBag.DeildType=GetString("Item.Heiti");
 
}

 

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?

 

/Hans

 

Replies

 
Nicolai Pedersen
Nicolai Pedersen
Reply

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";

And

Convert.ToString(HttpContext.Current.Items["Test"])
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Perfect Nicolai 👍🏻

Works like a charm thank you :)

/Hans

 

You must be logged in to post in the forum