Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 28, 2012 @ 23:26
    Kim Andersen
    0

    Generate link to a node selected in a content picker in v5

    Hi guys

    I'm trying something that I think should be fairly simple. I have a content picker (called "extraInfo") on a doc type of mine. In this picker I have choosen another node (let's call it related-node) which contains another content picker (called "link") and some other properties.

    What I want to achieve is to generate a link to the selected node in the "link"-property on the related-node.

    Right now I have the following code:

    var node1 = Umbraco.GetEntityById(HiveId.Parse((string)DynamicModel.extraInfo));

    The above code can be used to render some of the properties on the related-node like a textstring called "title" like this:

    @(node1.Attribute<string>("title"))

    But how do I generate a link to a node selected in a content picker?

    Thanks in advance!

    /Kim A

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 29, 2012 @ 13:26
    Sebastiaan Janssen
    0

    Assuming that DynamicModel.extraInfo give you a HiveID (like: p__nhibernate$_v__guid$_7a0cf5288bdb46d195539fd10125b2fb) you can do this a bit easier:

    @{ 
        string contentId = DynamicModel.extraInfo;
        dynamic content = Hive.Content.GetById(contentID);
    }

    <a href="@content.NiceUrl()">@content.Name</a>
  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jan 29, 2012 @ 13:39
    Tom Madden
    2

    Kim/Sebastiaan,

    You can use

    <a href="@Umbraco.GetUrl(DynamicModel.extraInfo)">@content.Name</a>

    Note I've changed it to Umbraco.GetUrl, rather than content.NiceUrl

    You'd also have to add a check that a value has been set first.

    Regards

    Tom

     

  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 29, 2012 @ 13:52
    Kim Andersen
    0

    Hi guys

    @Sebastiaan:

    When trying out your snippet I'm getting a YSOD:

    Server Error in '/' Application.
    'Umbraco.Cms.Web.Model.Content' does not contain a definition for 'NiceUrl'
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Cms.Web.Model.Content' does not contain a definition for 'NiceUrl'

    Source Error:

    Line 23:             <div class="sidebar-content">
    Line 24:            
    Line 25:                 <a href="@content.NiceUrl()">@content.Name</a>
    Line 26:                 @if (itm.Attribute<string>("title") != "")
    Line 27: 

    @content.Name does work perfectly though, rendering the name of the selected node. Don't now if I'm missing something? :S

     

    @Tom:

    Your example does work for me! Thanks mate.

    /Kim A

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jan 29, 2012 @ 14:01
    Morten Bock
    0

    Maybe it's a property and not a method?

    @content.NiceUrl

  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 29, 2012 @ 14:09
    Kim Andersen
    0

    Ohh, actually tried that as well, but with the same YSOD as shown above unfortunatelly :(

    Would be nice to figure this out, but for now I can live with the version Tom came with :)

    /Kim A

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 29, 2012 @ 14:20
    Sebastiaan Janssen
    2

    Sorry, mixing up my vars and my dynamics, corrected code:

    @{ 
       
    string contentId = DynamicModel.extraInfo;
        var content
    = Hive.Content.GetById(contentId);
    }

    <a href="@content.NiceUrl()">@content.Namea>

    @Tom while Umbraco.GetUrl exists, in this case I wouldn't use it as we already have the information in the "content" variable. Your code would mean an extra roundtrip to the database/cache and use a bit more memory.

    You could make it a bit shorter, but that makes it a bit les readable:

    @{
        var content = Hive.Content.GetById((string)DynamicModel.extraInfo);
    }
    <a href="@content.NiceUrl()">@content.Name</a>
  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 29, 2012 @ 16:46
    Kim Andersen
    0

    Ahh that works now Sebastiaan!

    Great examples. Thanks :)

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft