Copied to clipboard

Flag this post as spam?

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


  • Chris Compton 2 posts 22 karma points
    Feb 17, 2012 @ 04:15
    Chris Compton
    0

    Content Picker in u4.7 with Razor

    I'm just starting out with Umbraco on a project.  We are starting with 4.7.

    I am using Razor in the templates, and everything was going smoothly until I started looking at how to get the information on a page referenced by a Content Picker.  I'd like to be able to obtain things like the title and nice url.

    I've seen some information in the forums on doing this within u5, but I don't think those examples are suitable for the 4.7 environment.  Any advice on how to approach this?

  • Evan Jardine 108 posts 168 karma points
    Feb 17, 2012 @ 06:20
    Evan Jardine
    0

    Hi Chris

    Try using the umbraco.library.NiceUrl(int) method.

    @umbraco.library.NiceUrl(id)

    Cheers

    Evan

  • Ben Norman 167 posts 276 karma points
    Feb 17, 2012 @ 07:55
    Ben Norman
    0

    Get Media item with the following:

    var media = Model.MediaById({id});

    Get the Url for the media item with the following:

    var url = media.UmbracoFile;

     

  • Douglas Ludlow 210 posts 366 karma points
    Feb 17, 2012 @ 12:47
    Douglas Ludlow
    0

    If you're starting out a new project, then you must be using the latest version of Umbraco 4.7, version 4.7.1.1, right? And it sounds like you'd like to get content, not media. The content picker will store the id of the picked node as a string. If the alias of the content picker property is "content", then:

    var node = Library.NodeById(Model.content);

    ...will give you access to the node. node is now a DynamicNode, just like Model, so you have access to all its properties. Refer to the handy cheatsheet. For a link to the node using its title (name) and nice url:

    <a href="@node.NiceUrl">@node.Name</a>

    Hope that helps!

  • Chris Compton 2 posts 22 karma points
    Feb 17, 2012 @ 20:57
    Chris Compton
    0

    The node code was perfect!!  Thank you!

     

    Though I wasn't looking for media, thanks for that information too, because media is my next adventure.

     

     

  • Douglas Ludlow 210 posts 366 karma points
    Feb 17, 2012 @ 21:00
    Douglas Ludlow
    0

    No problem, glad I could help.

  • Stefan van Leusden 21 posts 73 karma points
    Mar 02, 2012 @ 16:11
    Stefan van Leusden
    0

    What do you think about this method?
    It works, but I'm just curious, maybe you would do it a different way :)

     @{
        var site = Model.AncestorOrSelf("Example1");
      
        if (site.HasProperty("**aliasname**") && !String.IsNullOrWhiteSpace(site.**aliasname**.ToString()))
        {
            var bla2 = new DynamicNode(site.**aliasname**);

            <a href="@bla2.NiceUrl">
               We like Umbraco
            </a>
        }
    }
  • Douglas Ludlow 210 posts 366 karma points
    Mar 02, 2012 @ 17:14
    Douglas Ludlow
    0

    I guess it's really user preference, but I'd probably do it this way:

    @{
        var site = Model.AncestorOrSelf("Example1");

        if (site.aliasName.GetType() != typeof(DynamicNull) && site.aliasName != "")
        {
            var bla2 = Library.NodeById(site.aliasName);

            <a href="@bla2.NiceUrl">
                We like Umbraco
            </a>
        }
    }

    It's not really that different... but I like using the Library methods instead of contructors, but that's just me. I also like checking for DynamicNull, which is what an alias would return if the node didn't have the property.

Please Sign in or register to post replies

Write your reply to:

Draft