Umbraco 7.6 RC Related Links - 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'link' or for 'isInternal'
I have a list of document types called newsItems I'm trying to display in a macro partial. They have a relatedLinks data type. I'm trying to display a simple list of the related links for each of the newsItems queried. I have successfully done this using similar code in previous versions of Umbraco but for some reason it's not working in a new site I'm developing using 7.6.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var query = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where("Visible && DocumentTypeAlias == @0", "newsItem").OrderBy("sortOrder desc");
foreach (var page in query)
{
<h1>@page.Headline</h1>
<p>@page.body</p>
if (page.HasValue("relatedLinks") && page.relatedLinks.ToString().Length > 2)
{
foreach (var item in page.relatedLinks)
{
var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
var linkTarget = (bool)item.newWindow ? "_blank" : null;
<p><a href="@linkUrl" target="@linkTarget">@item.caption</a></p>
}
}
}
}
This line is trowing the error 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'isInternal'
var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
If I take out the contitional and just do var linkUrl = item.link it says it doesn't containg a definition for 'link'
Have I overlooked something? or is there a different recommended method of doing something in 7.6?
I guess you started this site on v7.6. In this version Umbraco switched to the "UDI" format for storing links. For content these start with "umb://document/" and for media with "umb://media"
From your code I see you are trying to parse the json your self. But in v7.6 this wonderful package (https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/) by Jeavon Leopold got integrated in the core.
So you can do this in your view :
var relatedLinks = Model.GetPropertyValue
Thanks Dave, I was able to get it working that way. The links you provided were very helpful.
Now that I have that relatedLinks object, I see that it has the properties Caption, NewWindow, IsInternal, Type, and Link, and I was able to successfully use those within a foreach loop.
Is there any documentation somewhere that shows other ways to work with these types of objects? Specifically, I'm trying to get a count of the related links, and display only the first link. I have tried options typical of lists or IENumerables such as relatedLinks.Count() and relatedLinks[0] and relatedLinks.Any() and just get errors saying Umbraco.Web.Models.RelatedLinks does not contain those definitions.
Umbraco 7.6 RC Related Links - 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'link' or for 'isInternal'
I have a list of document types called newsItems I'm trying to display in a macro partial. They have a relatedLinks data type. I'm trying to display a simple list of the related links for each of the newsItems queried. I have successfully done this using similar code in previous versions of Umbraco but for some reason it's not working in a new site I'm developing using 7.6.
This line is trowing the error 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'isInternal'
If I take out the contitional and just do var linkUrl = item.link it says it doesn't containg a definition for 'link'
Have I overlooked something? or is there a different recommended method of doing something in 7.6?
If I switch from Dynamic to Typed, I get a completely different problem that I posted about here.
Hi Josh,
I guess you started this site on v7.6. In this version Umbraco switched to the "UDI" format for storing links. For content these start with "umb://document/" and for media with "umb://media"
From your code I see you are trying to parse the json your self. But in v7.6 this wonderful package (https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/) by Jeavon Leopold got integrated in the core.
So you can do this in your view : var relatedLinks = Model.GetPropertyValue
Also I see you are using the dynamic syntax in your views. I would advise you to use the strongly typed version : http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/
Since you are using v7.6 the modelsbuilder is activated by default and you can use strongly typed models in your view. You can read some more about the modelsbuilder here : http://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/
Dave
Thanks Dave, I was able to get it working that way. The links you provided were very helpful.
Now that I have that relatedLinks object, I see that it has the properties Caption, NewWindow, IsInternal, Type, and Link, and I was able to successfully use those within a foreach loop.
Is there any documentation somewhere that shows other ways to work with these types of objects? Specifically, I'm trying to get a count of the related links, and display only the first link. I have tried options typical of lists or IENumerables such as relatedLinks.Count() and relatedLinks[0] and relatedLinks.Any() and just get errors saying Umbraco.Web.Models.RelatedLinks does not contain those definitions.
Thanks!
Hi Josh,
This is the source code for the RelatedLinks object : https://github.com/umbraco/Umbraco-CMS/blob/dev-v7.6/src/Umbraco.Web/Models/RelatedLinks.cs
As you can see it implements/inherits
IEnumerable<RelatedLink>
Maybe adding the following using statement to your view will solve the issue:
Don't forget the post as a solution. This is new in 7.6 a lot of people will run into this issue and it's easier for them to find the solution.
Dave
Thanks! I got it working by adding these using statements:
and changing
to this:
Thanks again for your help! ...Josh
This should be marked as a solution.
It already is.
Dave
is working on a reply...