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?
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
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:
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:
The above code can be used to render some of the properties on the related-node like a textstring called "title" like this:
But how do I generate a link to a node selected in a content picker?
Thanks in advance!
/Kim A
Assuming that DynamicModel.extraInfo give you a HiveID (like: p__nhibernate$_v__guid$_7a0cf5288bdb46d195539fd10125b2fb) you can do this a bit easier:
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
Hi guys
@Sebastiaan:
When trying out your snippet I'm getting a YSOD:
@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
Maybe it's a property and not a method?
@content.NiceUrl
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
Sorry, mixing up my vars and my dynamics, corrected code:
@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:
Ahh that works now Sebastiaan!
Great examples. Thanks :)
/Kim A
is working on a reply...