Copied to clipboard

Flag this post as spam?

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


  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 18:25
    Andrew Unger
    0

    "System.NullReferenceException: Object reference not set to an instance of an object." error when trying to render tags

    Hello I am trying to render tags I have set up for a blog page but I am having trouble. I am using version 7.13. I have the tags set up in the doctype with a few test tags already filled out in the content manager. I am using a Partial View Macro File to render the tags in my navigation. I have tried using different syntax than this as well as different methods with no luck. I would appreciate any help with this. I will post my code with a screen shot of the error I am getting.

            @{var items = blogNode.GetPropertyValue<IEnumerable<IPublishedContent>>("tags");}           
             <ul>
                 @foreach(var item in items){
                    <li><a href="@item.Url">@item.Name</a></li>
                    }           
             </ul> 
    

    enter image description here

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 18:28
    Alex Skrypnyk
    1

    Hi Andrew,

    Use this code:

    @{var items = blogNode.GetPropertyValue<IEnumerable<string>>("tags");}
    @if (items != null)
    {
    <ul>
        @foreach (var item in items)
        {
            <li><a href="@item.Url">@item.Name</a></li>
        }
    </ul>
    }
    

    Thanks,

    Alex

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 18:31
    Andrew Unger
    0

    I tried that code and I am getting this error now

    enter image description here

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 18:35
    Alex Skrypnyk
    1

    Sorry, I did a mistake, each item in tags property is a string value

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 18:34
    Alex Skrypnyk
    100

    What about this code:

    @{var items = blogNode.GetPropertyValue<IEnumerable<string>>("tags");}
    @if (items != null)
    {
    <ul>
        @foreach (var item in items)
        {
            <li>@item</li>
        }
    </ul>
    }
    
  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 18:44
    Andrew Unger
    0

    The error went away but its not displaying the tags. I have multiple content nodes with the same doctype that has the tags property. Could having multiple nodes with that doctype and property be a reason why its not working?

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 18:46
    Alex Skrypnyk
    1

    no, you should see a list of tags with this code

    but you should change the logic that renders links, where the link should point?

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 18:54
    Andrew Unger
    0

    Yea I was going to do that. I am planning on using a query string and having the tag that is clicked be the value in the query string and using that to filter out results. I am still not sure why the tags aren't displaying though.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 19:37
    Alex Skrypnyk
    1
    @{var items = blogNode.GetPropertyValue<IEnumerable<string>>("tags");}
    @if (items != null)
    {
        <ul>
            @foreach (var item in items)
            {
                <li><a href="@Umbraco.AssignedContentItem.Url?tag=@item">@item</a></li>
            }
        </ul>
    }
    

    What about this code? it should create a list of links of the same page and "tag" query string param with actual tag value

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 19:44
    Andrew Unger
    0

    That didn't work either. Some of the content nodes don't have any tags filled out in the content manager. Most of them do have tags in there but could that cause issues? I know the alias is correct too so I'm not sure why its not working.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 20:02
    Alex Skrypnyk
    1

    we added this line to miss nodes that don't have tags property filled - @if (items != null)

    nodes that have this property should be rendered

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 20:02
    Alex Skrypnyk
    1

    Can you share all view code?

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 20:03
    Andrew Unger
    0
        @inherits Umbraco.Web.Macros.PartialViewMacroPage
            @{ 
                var node = Model.Content;
                var blogNode = node.AncestorOrSelf("blog");
                var home = node.Site();
            }
    
        <div id="blog-navigation" class="column small-12 medium-3">
            <div class="row">
                <div class="column small-12 line2">
                    <h2>@blogNode.GetPropertyValue("productsText", "Products")</h2>
                    <label>
                        <select class="select-search" placeholder="Select Product">
                            <option value="">Search...</option>
                            <option value="4">Thomas Edison</option>
                            <option value="1">Nikola</option>
                            <option value="3">Nikola Tesla</option>
                            <option value="5">Arnold Schwarzenegger</option>
                        </select>
                    </label>
                </div>
                <div id="blog-projects" class="column small-12 line2">
                    <h2>@blogNode.GetPropertyValue("projectsText", "Projects")</h2>
    
    
            @* @if(blogNode.HasValue("tags")){           
    
               }*@  
    
        @*@{var items = blogNode.GetPropertyValue<IEnumerable<IPublishedContent>>("tags");}
        @if (items != null)
        {
        <ul>
            @foreach (var item in items)
            {
                <li><a href="@item.Url">@item.Name</a></li>
            }
        </ul>
        }*@
    
        @{var items = blogNode.GetPropertyValue<IEnumerable<string>>("tags");}
        @if (items != null)
        {
            <ul>
                @foreach (var item in items)
                {
                    <li><a href="@Umbraco.AssignedContentItem.Url?tag=@item">@item</a></li>
                }
            </ul>
        }
    
    
        @*@if (@blogNode.HasProperty("tags")) { string[] tagsList = blogNode.tags;
    
        if (tagsList.Count() > 0)
        {
            <ul>
            @foreach (var tag in tagsList)
            {
                 <li>@tag</li>
            }
            </ul>
        }
        }*@
    
    
    
    
                    @*<ul>
                        <li><a href="#">Bath</a></li>
                        <li><a href="#">Closet</a></li>
                        <li><a href="#">Crafts</a></li>
                        <li><a href="#">Doors</a></li>
                        <li><a href="#">Kitchen</a></li>
                        <li><a href="#">Painting</a></li>
                        <li><a href="#">Walls</a></li>
                        <li><a href="#">Window</a></li>
                        <li><a href="#">Wood</a></li>
                    </ul>*@
                </div>
                @if (blogNode.HasValue("featured"))
                {
                    <div id="blog-featured" class="column small-12 line2">
                        <h2>@blogNode.GetPropertyValue("featuredText", "Featured") </h2>
                        <ul>
                            @foreach (var post in blogNode.GetPropertyValue<List<IPublishedContent>>("featured"))
                            {
                                <li><a href="@post.Url">@post.Name</a></li>
                            }
                        </ul>
                    </div>
                }
                <div id="blog-featured" class="column small-12 line2">
                    <h2>@blogNode.GetPropertyValue("socialText", "Connect With Us")</h2>
                    <ul class="social">
                        @if (home.HasValue("facebook"))
                        { <li><a href="@home.GetPropertyValue("facebook")"><img src="/images/orange_social_icon-facebook.svg" alt="facebook"></a></li> }
                        @if (home.HasValue("twitter"))
                        { <li><a href="@home.GetPropertyValue("twitter")"><img src="/images/orange_social_icon-twitter.svg" alt="twitter"></a></li> }
                        @if (home.HasValue("linkedin"))
                        { <li><a href="@home.GetPropertyValue("linkedin")"><img src="/images/orange_social_icon-linkedin.svg" alt="linkedin"></a></li> }
                        @if (home.HasValue("youtube"))
                        { <li><a href="@home.GetPropertyValue("youtube")"><img src="/images/orange_social_icon-youtube.svg" alt="youtube"></a></li> }
                        @if (home.HasValue("instagram"))
                        { <li><a href="@node.GetPropertyValue("instagram")"><img src="/images/orange_social_insta-logo.svg" alt="instagram"></a></li> }
                    </ul>
                </div>
    
    
            </div>
        </div>
    
  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 20:08
    Alex Skrypnyk
    1

    and what do you see on the page? is it just an empty space? maybe in markup?

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 20:10
    Andrew Unger
    0

    I didn't see anything when I inspected the webpage

  • Andrew Unger 45 posts 148 karma points
    Apr 30, 2020 @ 20:09
    Andrew Unger
    0

    yea its just empty space where its supposed to be.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Apr 30, 2020 @ 20:47
    Alex Skrypnyk
    1

    if empty space then - items == null

    Can you check in /app_data/umbraco.cofing exactly this field in exactly this node?

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 12:16
    Andrew Unger
    0

    Yea I have it open now what should I check about it?

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 12:30
    Andrew Unger
    0

    yea that was the path for that too

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    May 01, 2020 @ 13:46
    Alex Skrypnyk
    1

    try to find blogNode there and check "tags" property, is it list of strings?

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 14:06
    Andrew Unger
    0

    yea there is a list of strings where tag is

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 14:10
    Andrew Unger
    0

    Could I not be using the correct node when using GetPropertyValue? Currently I have var blogNode = node.AncestorOrSelf("blog"); and I'm using that blogNode, and I tried Model.Content too but the tags still aren't printing out

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 14:31
    Andrew Unger
    0

    Okay they are showing up now I figured it out. They are only getting displayed on the blog post page thats actually a page for the blog articles themselves, and they are not displaying on the blog page that has the links to those blog articles. I need to figure out how to get it to display on that blog page that has all the blog article links on it as well. Basically the user will click the tag and I am going to use jquery to print out links to articles that have that tag.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    May 01, 2020 @ 14:37
    Alex Skrypnyk
    1

    Ah, great, at least we know that the code provided works, now we need to adjust it

    Can you show the tree how it looks in umbraco backend?

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 14:48
    Andrew Unger
    0

    There is the top node blog that has the articles listed and then blog post is the child of that. Then that "blog" node is a child of a node called "About Us"

    enter image description here

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 15:03
    Andrew Unger
    0

    Okay so

       Model.Content.GetPropertyValue<IEnumerable<string>>("tags"); 
    

    will display the tags on the "blog article" page. "blogNode" doesn't display on either page.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    May 01, 2020 @ 15:41
    Alex Skrypnyk
    1

    It's because of Model.Content always is the current page, so on the blog page you don't have these properties but you have them in blog article.

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 16:06
    Andrew Unger
    0

    So then I would need to use something like this then?

    var blogPostNode = node.AncestorOrSelf("blogPost");
    
  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 16:11
    Andrew Unger
    1

    Okay I was able to figure it out. I used this and it works for both the blog page and blog article page

    var blogPostNode = node.DescendantOrSelf("blogPost");
    
  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    May 01, 2020 @ 16:14
    Alex Skrypnyk
    1

    oo nice solution, but just to let you know that DescendantOrSelf is a quite expensive method in terms of performance if there are too many nodes under the current node

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 16:22
    Andrew Unger
    0

    Thank you for that tip and for all your help with this you're a life savor as usual

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    May 01, 2020 @ 16:24
    Alex Skrypnyk
    1

    You are always welcome, have a great weekend, keep safe and keep asking questions :)

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 17:02
    Andrew Unger
    0

    So I did run into another issue. The only tags that show up are of the first child of the blog page. I need to get all the tags from all the child nodes of blog.

  • Andrew Unger 45 posts 148 karma points
    May 01, 2020 @ 20:20
    Andrew Unger
    1

    I ended up finding a solution to that last problem but its a little messy. Just in case anyone else has a similar issue this is how I solved only printing the tags once. I used a string to put all the tags into, and then I used .Distinct()); in that string to get rid of duplicates, and another for loop to print out all the tags that are distinct.

                    @{
                        var children = blogNode.Children.Where("Visible");
                    }
    
                    @foreach (var child in children)
                    {
                        var currentChild = child.GetPropertyValue<IEnumerable<string>>("tags");
                        if (currentChild != null)
                            {
                                <ul>
                                    @foreach (var item in currentChild)
                                    {
                                        childResults += @item + " ";
                                    }
                                </ul>
                            }
                    } 
    
                @{result = string.Join(" ", childResults.Split(' ').Distinct());
                string[] strSplit = result.Split();
                var last = strSplit.Last();
                }
                <ul>
                    @foreach(var eachResult in strSplit){
    
                        if (eachResult.Equals(last))
                        {
                            @*do nothing for the last entry because it prints out nothing*@
                        }
                        else{
                            <li><a href="@blogNode.Url?tag=@eachResult">@eachResult</a></li>
                        }
    
                    }
                </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft