I've gotten help to create these string-variables, but when I try to output the created fields on the document-type i get a funny response that I cannot figure out what means.
The Output that I get is this. I've pasted the HTML here:
var slide_Name = Array('Pic1', 'Pic2', 'Pic3'); var slide_headlines = Array('System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]'); var slide_linktext = Array('System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]'); and so on ...
I have a "folder" I have called settings (just an empty document-type). Underneath this "folder" have I created 3 documents of the type "PIC". I've called them Pic1, Pic2 and Pic3.
The document-type PIC contains the generic properties:
frontHeadline as a string frontLinkText as a string frontBoxText as a string carousselLink as a content-picker carousselimage as a media-picker
The "folder" has the id of: 1358, which i use in the dynamic node like this:
dynamic node =@Model.NodeById(1358);
Is there a way to call the @model.CarrousselFolder so that I get the id of the folder selected in the contentpicker instead of hardtyping the ID?
var slide_headlines = Array(Headline 1 line 1<br/>Headline 1 line 2,Headline 2 line 1<br/>Headline 2 line 2,Headline 3 line 1<br/>Headline 3 line 2); var slide_linktext = Array(Link text 1,Link text 2,Link text 3); var slide_bottom_txt = Array(Box text img 1,Box text img 2,Box text img 3); var slide_link = Array(1129,1177,1270); var slide_pics = Array(1056,1057,1058);
How do i change the id's to something useful? I need the URL's for the links and the images?
var slide_headlines = Array('Billede 1 linie 1 <br/>Billede 1 linie 2','Billede 2 linie 1 <br/>Billede 2 linie 2','Billede 3 linie 1 <br/>Billede 3 linie 2'); var slide_linktext = Array('Link text 1','Link text 2','Link text 3'); var slide_bottom_txt = Array('Box text billede 1','Box text billede 2','Box text billede 3'); var slide_link = Array('/personbiler-mercedes-benz/personbiler-mercedes-benz/alle-mercedes-modeller.aspx','/personbiler-renault/personbiler-renault/alle-renault-modeller.aspx','/tunge-erhvervskoeretoejer/tunge-erhvervskoeretoejer/se-alle-modellerne.aspx'); var slide_pics = Array('/media/83/slide1.jpg','/media/88/slide2.jpg','/media/93/slide3.jpg');
I tried to do something like this, but it's not working
var slide_headlines = Array('System.Collections.Generic.List`1[System.String]'); var slide_linktext = Array('System.Collections.Generic.List`1[System.String]'); var slide_bottom_txt = Array('System.Collections.Generic.List`1[System.String]'); var slide_link = Array('System.Collections.Generic.List`1[System.String]'); var slide_pics = Array('System.Collections.Generic.List`1[System.String]');
I would advise you not to render dynamic javascript from Razor, it gets really messy really fast.
The general concensus is that javascript should be as unobtrusive as possible, try (in a seperate javascript file) to read the values you need from the existing HTML and then using that. It's a bit harder to do, but very rewarding when you realize that you can just reuse the code on another page and don't have to repeat the razor codeblock there (so you're very DRY - Don't Repeat Yourself).
You are absolutly right that javascript should not be rendered inline with razor. If I need to use data from the cms I usually create a base method that returns json if not possible doing it with HTML.
Razor, string and children fields
Hi Gurus
I've gotten help to create these string-variables, but when I try to output the created fields on the document-type i get a funny response that I cannot figure out what means.
The code is like this:
The Output that I get is this. I've pasted the HTML here:
var slide_Name = Array('Pic1', 'Pic2', 'Pic3');
var slide_headlines = Array('System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]');
var slide_linktext = Array('System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]','System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]');
and so on ...
Can someone point me in the right direction?
Best regards
Jimmy Dan Mortensen
Hi Jimmy
Are the frontHeadline,frontLinkText,.... nodes or are they properties on a node (Pic1, Pic2, ... )
Dave
I have a "folder" I have called settings (just an empty document-type). Underneath this "folder" have I created 3 documents of the type "PIC". I've called them Pic1, Pic2 and Pic3.
The document-type PIC contains the generic properties:
The "folder" has the id of: 1358, which i use in the dynamic node like this:
Is there a way to call the @model.CarrousselFolder so that I get the id of the folder selected in the contentpicker instead of hardtyping the ID?
Hi Jimmy,
The problem with your code is that you are treating the properties as nodes.
This snippet should work (not tested)
Hi Dave
That worked like a charm :-)
The output I got looks like this :
var slide_headlines = Array(Headline 1 line 1<br/>Headline 1 line 2,Headline 2 line 1<br/>Headline 2 line 2,Headline 3 line 1<br/>Headline 3 line 2);
var slide_linktext = Array(Link text 1,Link text 2,Link text 3);
var slide_bottom_txt = Array(Box text img 1,Box text img 2,Box text img 3);
var slide_link = Array(1129,1177,1270);
var slide_pics = Array(1056,1057,1058);
How do i change the id's to something useful? I need the URL's for the links and the images?
Hi Jimmy,
This should work.I also tried to get the node id for the folder form the carrouselFolder property
I only have 1 major problem now.
I need ' outlining the output like the below:
var slide_headlines = Array('Billede 1 linie 1 <br/>Billede 1 linie 2','Billede 2 linie 1 <br/>Billede 2 linie 2','Billede 3 linie 1 <br/>Billede 3 linie 2');
var slide_linktext = Array('Link text 1','Link text 2','Link text 3');
var slide_bottom_txt = Array('Box text billede 1','Box text billede 2','Box text billede 3');
var slide_link = Array('/personbiler-mercedes-benz/personbiler-mercedes-benz/alle-mercedes-modeller.aspx','/personbiler-renault/personbiler-renault/alle-renault-modeller.aspx','/tunge-erhvervskoeretoejer/tunge-erhvervskoeretoejer/se-alle-modellerne.aspx');
var slide_pics = Array('/media/83/slide1.jpg','/media/88/slide2.jpg','/media/93/slide3.jpg');
I tried to do something like this, but it's not working
@Html.Raw(string.Format("var slide_pics = Array({0});", string.Join("',",@' + carousselimages)))
You probably need this
That changed the output to:
var slide_headlines = Array('System.Collections.Generic.List`1[System.String]');
var slide_linktext = Array('System.Collections.Generic.List`1[System.String]');
var slide_bottom_txt = Array('System.Collections.Generic.List`1[System.String]');
var slide_link = Array('System.Collections.Generic.List`1[System.String]');
var slide_pics = Array('System.Collections.Generic.List`1[System.String]');
Oops my mistake.
This should solve it
Thank you so much :-)
You litterally saved my day :-)
Just a general remark here:
I would advise you not to render dynamic javascript from Razor, it gets really messy really fast.
The general concensus is that javascript should be as unobtrusive as possible, try (in a seperate javascript file) to read the values you need from the existing HTML and then using that. It's a bit harder to do, but very rewarding when you realize that you can just reuse the code on another page and don't have to repeat the razor codeblock there (so you're very DRY - Don't Repeat Yourself).
Hi Sebastiaan,
You are absolutly right that javascript should not be rendered inline with razor. If I need to use data from the cms I usually create a base method that returns json if not possible doing it with HTML.
I was just helping Jimmy to get his code running.
Dave
Anybody got any ideas as to why the above mentioned code works on my local-installation, but when I deploy to the host it throws this error at me?
Error loading Razor Script KarrusselFront.cshtml
Cannot instantiate a DynamicNode without an id
And any pointers on how to fetch the variables from inside a JS-file as Sebastiaan writes?
Is the node selected on your content item ?
Unfortunately yes :-( That was the first thing i checked.
Can you try if it works when you hardcode the node id ?
it does :-)
If i take DynamicNode node = new DynamicNode(Current.GetPropertyValue("KarrusselFolder")); and change it to:
DynamicNode node = new DynamicNode(1173);
it works perfectly :-)
Shouldn't I be able to simply write:
DynamicNode node = new DynamicNode(@Model.Propertyalias);
What needs to be fullfilled in order to use the custom properties by typing @Model.PropertyAlias??
You should be able to use
DynamicNode node = new DynamicNode(Model.KarrusselFolder)
I prefer using the the DynamicNode so you get intellisense in VS
That gives me the exact same error :-(
Error loading Razor Script KarrusselFront.cshtml
Cannot instantiate a DynamicNode without an id
And I've doublechecked that I've spelled the property correctly as I've copy/pasted ;-) It's called karrusselFolder on the document-type
Can you try to output only the value of the property so we can make sure it contains a value ?
It doesn't ???
This
<umbraco:Macro runat="server" language="cshtml">
<p>Dette er mit id: @Model.karrusselFolder</p>
</umbraco:Macro>
creates
Dette er mit id: 0
Can you assign the foler again on the document and publish it ?
I don't exactly know what I have done, but know it's working :-)
I've removed the property, changed the way I spelled it, and a couple of other things.And know it's working :-)
is working on a reply...