Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 10:25
    Jimmy Dan Mortensen
    0

    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:

     


    @inherits
    umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines
    @
      dynamic node = @Model.NodeById(1358);
      
      var slide_Name = string.Format(
         "var slide_Name = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + Name + \"'\"")));
      
      var slide_headlines = string.Format(
         "var slide_headlines = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + frontHeadline + \"'\"")));
      
      var slide_linktext = string.Format(
         "var slide_linktext = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + frontLinkText + \"'\"")));
      
      var slide_bottom_txt = string.Format(
         "var slide_bottom_txt = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + frontBoxText + \"'\"")));
      
      var slide_link = string.Format(
         "var slide_link = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + carousselLink + \"'\"")));
      
      var slide_pics = string.Format(
         "var slide_pics = Array({0});",
         string.Join(", ", node.Children.Select("\"'\" + carousselimage + \"'\"")));
    }

    <script>
      @Html.Raw(slide_Name)
      @Html.Raw(slide_headlines)
      @Html.Raw(slide_linktext)
      @Html.Raw(slide_bottom_txt)
      @Html.Raw(slide_link)
      @Html.Raw(slide_pics)
    </script>

    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

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 11:30
    Dave Woestenborghs
    0

    Hi Jimmy

    Are the frontHeadline,frontLinkText,.... nodes or are they properties on a node (Pic1, Pic2, ... )

    Dave

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 11:37
    Jimmy Dan Mortensen
    0

    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?

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 12:00
    Dave Woestenborghs
    0

    Hi Jimmy,

    The problem with your code is that you are treating the properties as nodes.

    This snippet should work (not tested)

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
     
        DynamicNode node = new DynamicNode(1358);
            
        DynamicNodeList pics = node.GetChildrenAsList;
     
        var names = new List<string>();
        var headlines = new List<string>();
        var linktexts = new List<string>();
        var boxtexts = new List<string>();
        var caroussellinks = new List<string>();
        var carousselimages = new List<string>();
     
        pics.Items.ForEach(delegate(DynamicNode p)
                               {
                                   names.Add(p.Name);
                                   headlines.Add(p.GetPropertyValue("frontHeadline"));
                                   linktexts.Add(p.GetPropertyValue("frontLinkText"));
                                   boxtexts.Add(p.GetPropertyValue("frontBoxText"));
                                   caroussellinks.Add(p.GetPropertyValue("carousselLink"));
                                   carousselimages.Add(p.GetPropertyValue("carousselimage"));
                               });
     
    }
    <script type="text/javascript">
    @Html.Raw(string.Format("var slide_Name = Array({0});", string.Join(",",names)))
    @Html.Raw(string.Format("var slide_headlines = Array({0});", string.Join(",",headlines)))
    @Html.Raw(string.Format("var slide_linktext = Array({0});", string.Join(",",linktexts)))
    @Html.Raw(string.Format("var slide_bottom_txt = Array({0});", string.Join(",",boxtexts)))
    @Html.Raw(string.Format("var slide_link = Array({0});", string.Join(",",caroussellinks)))
    @Html.Raw(string.Format("var slide_pics = Array({0});", string.Join(",",carousselimages)))
    </script>
  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 12:11
    Jimmy Dan Mortensen
    0

    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?

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 12:19
    Dave Woestenborghs
    0

    Hi Jimmy,

    This should work.I also tried to get the node id for the folder form the carrouselFolder property

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
    
        DynamicNode node = new DynamicNode(Current.GetPropertyValue("carrouselFolder"));
    
        DynamicNodeList pics = node.GetChildrenAsList;
    
        var names = new List<string>();
        var headlines = new List<string>();
        var linktexts = new List<string>();
        var boxtexts = new List<string>();
        var caroussellinks = new List<string>();
        var carousselimages = new List<string>();
    
    
        pics.Items.ForEach(delegate(DynamicNode p)
                               {
                                   names.Add(p.Name);
                                   headlines.Add(p.GetPropertyValue("frontHeadline"));
                                   linktexts.Add(p.GetPropertyValue("frontLinkText"));
                                   boxtexts.Add(p.GetPropertyValue("frontBoxText"));
                                   caroussellinks.Add(new DynamicNode(p.GetPropertyValue("carousselLink")).Url);
                                   carousselimages.Add(p.Media("carousselimage","umbracoFile"));
                               });
    
    }
    <script type="text/javascript">
    @Html.Raw(string.Format("var slide_Name = Array({0});", string.Join(",",names)))
    @Html.Raw(string.Format("var slide_headlines = Array({0});", string.Join(",",headlines)))
    @Html.Raw(string.Format("var slide_linktext = Array({0});", string.Join(",",linktexts)))
    @Html.Raw(string.Format("var slide_bottom_txt = Array({0});", string.Join(",",boxtexts)))
    @Html.Raw(string.Format("var slide_link = Array({0});", string.Join(",",caroussellinks)))
    @Html.Raw(string.Format("var slide_pics = Array({0});", string.Join(",",carousselimages)))
    </script>
  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 13:01
    Jimmy Dan Mortensen
    0

    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)))

     

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 13:31
    Dave Woestenborghs
    0

    You probably need this

    @Html.Raw(string.Format("var slide_pics = Array({0});",string.Join(",",string.Format("'{0}'",carousselimages))))
  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 13:33
    Jimmy Dan Mortensen
    0

    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]');

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 13:38
    Dave Woestenborghs
    0

    Oops my mistake.

    This should solve it

       pics.Items.ForEach(delegate(DynamicNode p)
                               {
                                   names.Add(p.Name);
                                   headlines.Add(p.GetPropertyValue("frontHeadline"));
                                   linktexts.Add(p.GetPropertyValue("frontLinkText"));
                                   boxtexts.Add(p.GetPropertyValue("frontBoxText"));
                                   caroussellinks.Add(new DynamicNode(p.GetPropertyValue("carousselLink")).Url);
                                   carousselimages.Add(string.Format("'{0}'",p.Media("carousselimage","umbracoFile")));
                               });
        
  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 13, 2011 @ 13:56
    Jimmy Dan Mortensen
    0

    Thank you so much :-)
    You litterally saved my day :-)

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 13, 2011 @ 15:41
    Sebastiaan Janssen
    0

    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).

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 13, 2011 @ 18:17
    Dave Woestenborghs
    1

    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

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:10
    Jimmy Dan Mortensen
    0

    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?

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 15, 2011 @ 12:11
    Dave Woestenborghs
    0

    Is the node selected on your content item ?

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:12
    Jimmy Dan Mortensen
    0

    Unfortunately yes :-( That was the first thing i checked.

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 15, 2011 @ 12:14
    Dave Woestenborghs
    0

    Can you try if it works when you hardcode the node id ?

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:16
    Jimmy Dan Mortensen
    0

    it does :-)

    If i take DynamicNode node = new DynamicNode(Current.GetPropertyValue("KarrusselFolder")); and change it to:

    DynamicNode node = new DynamicNode(1173);

    it works perfectly :-)

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:23
    Jimmy Dan Mortensen
    0

    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??

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 15, 2011 @ 12:27
    Dave Woestenborghs
    0

    You should be able to use 

    DynamicNode node new DynamicNode(Model.KarrusselFolder)

    I prefer using the the DynamicNode so you get intellisense in VS

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:33
    Jimmy Dan Mortensen
    0

    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

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 15, 2011 @ 12:35
    Dave Woestenborghs
    0

    Can you try to output only the value of the property so we can make sure it contains a value ?

     

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 12:38
    Jimmy Dan Mortensen
    0

    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

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 15, 2011 @ 12:43
    Dave Woestenborghs
    0

    Can you assign the foler again on the document and publish it ?

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Dec 15, 2011 @ 13:06
    Jimmy Dan Mortensen
    0

    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 :-)

     

Please Sign in or register to post replies

Write your reply to:

Draft