Developer forum

Forum » CMS - Standard features » ViewBag not working to carry variables across templates

ViewBag not working to carry variables across templates

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

In a pagetemplate I have this

@{
ViewBag.PageType=GetString("Item.PageType.Value");

}

 

On a paragraph template on the same page, I try to publish the value of the vievbag, like this @ViewBag.PageType

Anybody who can tell me what I am doing wrong?

 

/Hans


Replies

 
Nicolai Pedersen
Reply

It is because it is 2 different instances of template engines and you can therefore not share the viewbag.

But in the paragraph template you can just go something like @PageView.Item.GetString("Item.PageType.Value")

BR Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Ok, thank you Nicolai

I have tried that, but now I get the error

Line 48: An object reference is required for the non-static field, method, or property 'Dynamicweb.Frontend.PageView.Item.get'

How do I fix this?

 

/Hans

 
Nicolai Pedersen
Reply

Please post your entire code block, and I can take a look

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

yes, the code is like this

 

@using System.Web;
@using Dynamicweb;
@using System;
@using Dynamicweb.Frontend;

<div class="row">
           <div class="col-12 my-4 nowpadding">
             <div class="list-group linklistbox">
               
              @PageView.Item.GetString("Item.PageType.Value")
               @if(GetBoolean("Item.VisYvirskrift")==true){<text><header>Her kanst tú</header></text>}
               
               @foreach(LoopItem i in GetLoop("Item.Leinki")){
            
            
               <text>
              <a href="@i.GetString("Item.Leinki.Leinki")" class="list-item right-arrow">@i.GetString("Item.Leinki.Tekstur")</a>
               </text>
                 }
            </div>
         </div>
       </div>
               

 

 

Just trying to output this in a paragraphtemplate. Was thinking maybe it was a missing assembly, but can´t get it to work.

 

/Hans

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Try to use @Pageview (lower case v) instead of @PageView OR use @Dynamicweb.Frontend.PageView.Current()

/Morten

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thanx for the reply Morten.

It doesn´t work unfortunately. Tried with lowercase v, and same result

this

     @Dynamicweb.Frontend.PageView.Current("Item.PageType")

 

Gives an error

Line 44: No overload for method 'Current' takes 1 arguments

 

 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

As the error states the method Current does not take any arguments.

@{

var pv = Dynamicweb.Frontend.PageView.Current();

var pageType =  pv.Item["PageType"] as string;

}

If you use IntelliSense it will be much easier to implement templates. Alternatively you can take a look at the API docs to see the available methods and properties.

/Morten

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Brilliant Morten, thank you :)

would love to use IntelliSense, but I am afraid i´m gonna have to stay in the dark until Dynamicweb will run .Net Core, as I am on and in a Mac Only Enviroment.

 

/Hans

 

You must be logged in to post in the forum