Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 343 karma points
    Jul 04, 2014 @ 17:14
    Tom Cowling
    0

    Simple answer?

    Pretty straight forward question really.

    I have an ultimate picker set up called centreAuthors, but when I add @Model.centreAuthors to the template I get the ids for each of the nodes picked. I've tried a few different . syntaxes like getproperty etc but can't seem to find the correct syntax.

    Any help would be much appreciated!

    Thanks,

    Tom

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 04, 2014 @ 19:02
    Jeavon Leopold
    1

    Hopefully the simple answer you seek is here?

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 15:56
    Tom Cowling
    0

    I've looked through that page before but couldn't work out what it was telling me to do..

    Which code should I be basing my solution on? The Typed example?

    Thanks,

    Tom

  • Charles Afford 1163 posts 1709 karma points
    Jul 07, 2014 @ 16:14
    Charles Afford
    0

    The picker will return node ids.  What you need to do is create an instance of a node or IPublishedContent (depends on version) using NodeFactory or ContentService and pass in the id you get back as the parameter.  This will give you an object with properties on :).

    You will need to split the ids on the ',' so

    IEnumurable<int> authors = Model.createAuthors.Split(',')

    Charlie :)

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 16:22
    Jeavon Leopold
    0

    @Tom, try this

    @{
        var dynamicUltimatePicker = CurrentPage.centreAuthors.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        var dynamicUltimateCollection = Umbraco.Content(dynamicUltimatePicker);
        foreach (var item in dynamicUltimateCollection)
        {     
            <p>@item.Name</p>         
        }       
    }
    

    @Charlie, you should never use the ContentService on a front end website for querying data, it is a management API. You should be using @Umbraco.Content or @Umbraco.TypedContent

    Jeavon

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 16:26
    Jeavon Leopold
    0

    @Tom, please note that this snippet should be used in a MVC view, partial view or a partial view macro.

    If you are using a legacy Razor Macro (Scripting File) then the solution you need is the DynamicNode example on the documentation page. However, if you can you should change to one of the supported methods.

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 16:28
    Tom Cowling
    0

    This is at the top of the page:

    @inherits umbraco.MacroEngines.DynamicNodeContext


    I guess that means I need to use the dynamicNode example?

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 16:45
    Tom Cowling
    0

    My brain's fried and I think I'm substituting the wrong aliases into the code! :(

    I have a razor macro and what I'm looking to replace is the following:

             <umbraco:Macro  runat="server" language="cshtml">
            @inherits umbraco.MacroEngines.DynamicNodeContext
            @{
            if (Model.HasValue("centreAuthors"))
                {
                                <tr>
                    <td valign="top">
                        <p><strong>Centre Authors:</strong></p>
                    </td>
                    <td valign="top">                                                        
                    <p>@Model.centreAuthors</p>
                    </td>
                    </tr>
            }
            }                
    </umbraco:Macro>

    centreAuthors is the name of the property
    Authors is the name of the datatype which is an ultimate picker (not sure if you need that information?)

    I understand the method of reading in the CSVs from the picker and splitting them up using a for loop, but I very rarely code using razor so I'm not sure which bits to keep and which bits to ditch and where to put everything. All I seem to get no matter what I do is a big error splatted into the centre of the page...

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 17:20
    Jeavon Leopold
    0

    Ok, fear not Tom, I will post a solution for you. What version of Umbraco are you running?

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 17:23
    Tom Cowling
    0

    Thanks Jeavon. Using v6.2.1

    Just out of interest, am I close with this?

            if (Model.HasValue("centreAuthors"))
                {
                                <tr>
                    <td valign="top">
                        <p><strong>Centre Authors:</strong></p>
                    </td>
                    <td valign="top">                                                        
                IEnumerable<DynamicNode> PublishedNodeList = Library.NodesById(Model.centreAuthors.Split(','));       
                PublishedNodeList = PublishedNodeList.Where(x => x.GetType() != typeof(DynamicNull) && x.Id > 0);                       
                dynamic centreAuthors = new DynamicNodeList(PublishedNodeList);   
       
                if (centreAuthors.Any()){           
                    foreach(var item in centreAuthors.Where("Visible")){                  
                        <p>@item.Name</p>     
                    }              
                }
                    </td>
                    </tr>
            }
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 17:35
    Jeavon Leopold
    0

    It looks very close to me! I only have my mobile phone right now so I can't test I to find the issue is, but I will later. Does the error say anything useful?

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 17:40
    Tom Cowling
    0

    Annoyingly no..

     

    Error loading MacroEngine script (file: )

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 17:52
    Jeavon Leopold
    0

    In /config/umbracoSettings.config there is a setting to control how macro engine errors are managed, you could change this to "throw" to see if you get something more meaningful

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 18:06
    Tom Cowling
    0

    Nope... Just the same..

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 19:59
    Jeavon Leopold
    100

    Ah ok, I've just checked it out and it was only a missing code block.

    Try this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines
    @{
        if (Model.HasValue("centreAuthors"))
        {
            <tr>
                <td valign="top">
                    <p><strong>Centre Authors:</strong></p>
                </td>
                <td valign="top">
                    @{
                        IEnumerable<DynamicNode> PublishedNodeList = Library.NodesById(Model.centreAuthors.Split(','));
                        PublishedNodeList = PublishedNodeList.Where(x => x.GetType() != typeof(DynamicNull) && x.Id > 0);
                        dynamic centreAuthors = new DynamicNodeList(PublishedNodeList);
    
                        if (centreAuthors.Any())
                        {
                            foreach (var item in centreAuthors.Where("Visible"))
                            {
                                <p>@item.Name</p>
                            }
                        }
                    }
                </td>
            </tr>
        }
    }
    
  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 22:21
    Tom Cowling
    0

    Perfect. :)

    Thanks a lot for your help.

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 22:42
    Jeavon Leopold
    0

    You're very welcome! Can I ask, are you building a new site in Umbraco v6.2.1 or is it a upgrade of a old site?

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 22:45
    Tom Cowling
    0

    Building a new site - why?

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 07, 2014 @ 22:49
    Jeavon Leopold
    0

    It's because the method you are using for rendering here "Razor Macro" is legacy. It is recommended that you use a Partial View Macro if you are using WebForms templates (which it looks like you are) or direct MVC templates/partials. However there is currently a lot of confusion and many beginners start using the legacy method and also there is a large body of samples. Hopefully in v7.2 this will be reduced as the legacy "Razor Macros" will be hidden and in v7 MVC is the default rendering engine.

  • Tom Cowling 144 posts 343 karma points
    Jul 07, 2014 @ 22:54
    Tom Cowling
    1

    Ah. Thanks for the tipoff.

    I'll look into the newer stuff when I get a chance. I agree - it does get confusing.

Please Sign in or register to post replies

Write your reply to:

Draft