"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.
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?
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.
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.
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
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.
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"
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
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.
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>
"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.
Hi Andrew,
Use this code:
Thanks,
Alex
I tried that code and I am getting this error now
Sorry, I did a mistake, each item in tags property is a string value
What about this code:
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?
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?
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.
What about this code? it should create a list of links of the same page and "tag" query string param with actual tag value
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.
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
Can you share all view code?
and what do you see on the page? is it just an empty space? maybe in markup?
I didn't see anything when I inspected the webpage
yea its just empty space where its supposed to be.
if empty space then - items == null
Can you check in /app_data/umbraco.cofing exactly this field in exactly this node?
Yea I have it open now what should I check about it?
yea that was the path for that too
try to find blogNode there and check "tags" property, is it list of strings?
yea there is a list of strings where tag is
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
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.
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?
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"
Okay so
will display the tags on the "blog article" page. "blogNode" doesn't display on either page.
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.
So then I would need to use something like this then?
Okay I was able to figure it out. I used this and it works for both the blog page and blog article page
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
Thank you for that tip and for all your help with this you're a life savor as usual
You are always welcome, have a great weekend, keep safe and keep asking questions :)
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.
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.
is working on a reply...