For anyone reading this later on, my problem was that I was not taking care of null references. Especially in the foreach loop I added:
<ul class="level-4 list-inline">
@foreach (var subLink in customItem.SubLink)
{
<li>
<a href="@Url.PageUrl(subLink.Href)" target="@subLink.Target" title="@subLink.Title">@subLink.Text <span class="arrow"></span></a>
</li>
}
</ul>
So I modified like so:
@if (customItem.SubLink != null) {
<ul class="level-4 list-inline">
@foreach (var subLink in customItem.SubLink)
{
<li>
<a href="@Url.PageUrl(subLink.Href)" target="@subLink.Target" title="@subLink.Title">@subLink.Text <span class="arrow"></span></a>
</li>
}
</ul>
}
And it worked.
Hi,
I think you're nearly there with that last approach. I suspect the issue is that you need some null checking on the attributes of the link like this:
<ul class="level-4 list-inline">
@foreach (var subLink in customItem.SubLink)
{
<li>
<a href="@Url.ContentUrl(new EPiServer.Url(subLink.Href))" target="@(subLink.Target ?? "")" title="@(subLink.Title ?? "")">@(subLink.Text ?? "") <span class="arrow"></span></a>
</li>
}
</ul>
In the example, I've also used ContentUrl rather than PageUrl as PageUrl is there for legacy backwards-compatibility reasons.
Thank you very much for the suggestion Paul, that is an even more complete check for null references. I will incorporate that in my code.
But in your example, there does not appear to be a check for null references in ContentUrl? Am I correct?
Yes, you're correct. I'd assumed the null check had been done before that point. Putting it all together it would be:
@if (customItem.SubLink != null) {
<ul class="level-4 list-inline">
@foreach (var subLink in customItem.SubLink)
{
<li>
<a href="@Url.ContentUrl(new EPiServer.Url(subLink.Href))" target="@(subLink.Target ?? "")" title="@(subLink.Title ?? "")">@(subLink.Text ?? "") <span class="arrow"></span></a>
</li>
}
</ul>
}
I have a ContentArea called CustomNavigationItems.
This contains the following:
In my Navigation.cshtml I have this:
Right after the
I would like to access the link items within SubLink, but I am having a hell of a hard time grasping it.
I would like to do something like this:
I have tried accessing:
@customItem.SubLink
but the ouput is:<ul><li><a href="/link/d76209b3121e4033ac5b80dec57cd592.aspx">Test Link</a></li></ul>
I have also tried this:
But the result is the following error when trying to load the website:
I would like to be able to access the actual individual elements of the links contained within the
LinkItemCollection
group calledSubLink
.And would really appreciate any help in this direction.