I have a Link Picker property on my document type which allows the user to enter an external URL value or pick a content item from the Umbraco site (property name PrivacyContentLink
Within Razor, how do I get the URL regardless of which has been entered?
I have tried
if (Model.SiteRoot.GetProperty("PrivacyContentLink").HasValue)
{
var privacyNodeId = Model.Content.GetPropertyValue<int>("PrivacyContentLink");
privacyLink = umbraco.library.NiceUrl(privacyNodeId);
}
And...
if (Model.SiteRoot.GetProperty("PrivacyContentLink ").HasValue)
{
tcLink = Umbraco.TypedContent(Model.Content.GetPropertyValue<int>(" PrivacyContentLink ")).Url;
}
All with no luck
What is the best Razor approach to obtain the value to add to my anchor href attribute?
If it that one, use this code, one of this codes has to work:
// Strongly typed
LinkPicker link = Model.Content.GetPropertyValue("link");
// ModelsBuilder
var site = Model.Content.Site().OfType();
var link = site.Link;
var linkPicker = JsonConvert.DeserializeObject<LinkPickerModel>(Model.Content.GetPropertyValue<string>("PrivacyContentLink"));
if (linkPicker != null)
{
<a href="@linkPicker.Url" target="@linkPicker.Target" class="link">@linkPicker.Name<span></span></a>
}
Also add this class:
public class LinkPickerModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Target { get; set; }
public string Hashtarget { get; set; }
}
I think the Link Picker package should still work in latest Umbraco versions - we are using it in several projects.
Previously you had to include you own class (either inside the razor view or in a separate class file) to reflect the json data and then serialize the json data to this model.
Get URL from Link Picker property type
I have a Link Picker property on my document type which allows the user to enter an external URL value or pick a content item from the Umbraco site (property name
PrivacyContentLink
Within Razor, how do I get the URL regardless of which has been entered?
I have tried
And...
All with no luck
What is the best Razor approach to obtain the value to add to my anchor href attribute?
Hi Mike
What type of property editor are you using?
Is it Link Picker - https://our.umbraco.org/projects/backoffice-extensions/link-picker/?
/Alex
If it that one, use this code, one of this codes has to work:
Yes, it is the Gibe.LinkPicker property type. Thanks Alex
I don't understand your final reply - how do I get the URL of the selected value?
So if I insert www.google.com into the URL or select an existing Umbraco node?
Why are you not using Related Links?
https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Related-Links2
Link Picker is not compatible with latest Umbraco.
Which one is not compatible? I am running 7.5.9 at present
When I debug, I can get a handle on the property and see there is a value
How do I retrieve the url value from the
JPropertyList
?This one should work:
Also add this class:
Hi Mike
I think the Link Picker package should still work in latest Umbraco versions - we are using it in several projects.
Previously you had to include you own class (either inside the razor view or in a separate class file) to reflect the json data and then serialize the json data to this model.
However if you are using latest version of the package it should have this class included https://github.com/Gibe/Umbraco-Link-Picker/blob/master/src/Gibe.LinkPicker.Umbraco/Models/LinkPicker.cs and also the property value converter https://github.com/Gibe/Umbraco-Link-Picker/blob/master/src/Gibe.LinkPicker.Umbraco/PropertyConverters/LinkPickerValueConverter.cs
So you should be able to use this:
or with ModelsBuilder enabled:
where "link" is your property alias.
However we are mostly using another package Multi Url Picker: https://our.umbraco.org/projects/backoffice-extensions/multi-url-picker/ since it can be configurated with minimum and maximum links (so it can act both as Single and Multi Url Picker) and it also seems to work better on Umbraco Cloud.
Furthermore there is an open pull request for this on Umbraco repository https://github.com/umbraco/Umbraco-CMS/pull/2323 .. so it may be included in core in an upcoming release.
/Bjarne
is working on a reply...