Error when filtering on multi-select dropdown menu
I'm encountering a null reference error when I'm attempting to perform a filter based on values stored in a page's multi-select dropdown. I have this code in my template:
var selection = Model.Content.Children.Where("Visible").ToList();
var filterCategories = new List<string>();
var filterProjectTypes = new List<string>();
if (Request.QueryString["category"] != null)
{
filterCategories = Request.QueryString["category"].Split(',').ToList();
}
if (Request.QueryString["projectType"] != null)
{
filterProjectTypes = Request.QueryString["projectType"].Split(',').ToList();
}
if (filterCategories.Any())
{
selection = selection.Where(x => x.GetPropertyValue<string>("category").Split(',').Any(y => filterCategories.Contains(y))).ToList();
}
if (filterProjectTypes.Any())
{
selection = selection.Where(x => x.GetPropertyValue<string>("projectType").Split(',').Any(y => filterProjectTypes.Contains(y))).ToList();
}
So what's happening here is the page is pulling querystring values into lists and comparing them to the values stored in different multi-select dropdowns stored on each node. The filter on the "category" property works fine, but when it hits the exact same operation for the "projectTypes" property it gets a null reference error.
I did some further testing and found that it's the .Split(',') operation that's causing it. For some reason it's not seeing the result of x.GetPropertyValue<string>("projectType") as an actual string. It seems to be...nothing. When I pull the value individually and attempt to perform any object operations on it, even a GetType(), I get the same error.
It's odd that it doesn't have this issue with the "category" property but any other similar properties will give me the same error.
Just to add to the mystery, I can make the page display these properties by grabbing them and looping through them, so I know I have the alias names right. As long as I'm not performing any other operations on them they show up on the page just fine.
why it's only seeing the category property as a string where the other properties seem to be type-less.
I'm not sure I follow. What properties seem to be type-less? What observation leads you to believe they are type-less? What do you mean by "type-less" (e.g., dynamic, anonymous, casted as object, etc.)?
When I select the property into a List<string> then loop through it and display it on the page, it displays fine until I attempt to do any type-specific actions on it.
So if I do this:
var thingy = selection.Select(x => x.GetPropertyValue<string>("projectType")).ToList();
Then in the HTML of the page, do this:
@foreach(var thing in thingy)
{
<p>@thing</p>
}
It lists the comma separated list of the projectType properties of each child node no problem. But if I do this:
@foreach(var thing in thingy)
{
<p>@thing.ToString()</p>
}
I get the null reference error. I can't even do thing.GetType().
However, if I change thingy to be a List<string> pulled from the category properties of each node, I can do ToString(), Split(), Substring(), etc.
Both projectType and category properties are being stored in a "Dropdown list multiple", which should return a string of comma separated values, but only the category property is treated as if it's a string.
Ah. I don't think it has anything to do with the type. In both instances, it is a string.
The reason you are getting the null reference exception is because one of the nodes contains no projectType value. Because it is a null value, calling thing.ToString() produces a null reference exception, as you are attempting to call an instance function on a null value.
In other words, it is a string; it's just a null string. That is why my suggestion worked; because it filters out any null values. It may not have been apparent because you wouldn't be able to see the null value visibly with this code:
<p>@thing</p>
It would be more apparent if you did this instead:
<p>@(thing == null ? "null value" : thing)</p>
If it is null, you will see "null value" rather than nothing at all.
Error when filtering on multi-select dropdown menu
I'm encountering a null reference error when I'm attempting to perform a filter based on values stored in a page's multi-select dropdown. I have this code in my template:
So what's happening here is the page is pulling querystring values into lists and comparing them to the values stored in different multi-select dropdowns stored on each node. The filter on the "category" property works fine, but when it hits the exact same operation for the "projectTypes" property it gets a null reference error.
I did some further testing and found that it's the .Split(',') operation that's causing it. For some reason it's not seeing the result of x.GetPropertyValue<string>("projectType") as an actual string. It seems to be...nothing. When I pull the value individually and attempt to perform any object operations on it, even a GetType(), I get the same error.
It's odd that it doesn't have this issue with the "category" property but any other similar properties will give me the same error.
Just to add to the mystery, I can make the page display these properties by grabbing them and looping through them, so I know I have the alias names right. As long as I'm not performing any other operations on them they show up on the page just fine.
Try this:
Are you a wizard? That worked like a charm. Thanks! That's been bugging me for weeks.
I'm still curious about why it's only seeing the category property as a string where the other properties seem to be type-less.
I'm not sure I follow. What properties seem to be type-less? What observation leads you to believe they are type-less? What do you mean by "type-less" (e.g., dynamic, anonymous, casted as object, etc.)?
When I select the property into a
List<string>
then loop through it and display it on the page, it displays fine until I attempt to do any type-specific actions on it.So if I do this:
Then in the HTML of the page, do this:
It lists the comma separated list of the
projectType
properties of each child node no problem. But if I do this:I get the null reference error. I can't even do
thing.GetType()
.However, if I change
thingy
to be aList<string>
pulled from thecategory
properties of each node, I can doToString()
,Split()
,Substring()
, etc.Both
projectType
andcategory
properties are being stored in a "Dropdown list multiple", which should return a string of comma separated values, but only thecategory
property is treated as if it's a string.Ah. I don't think it has anything to do with the type. In both instances, it is a string.
The reason you are getting the null reference exception is because one of the nodes contains no
projectType
value. Because it is a null value, callingthing.ToString()
produces a null reference exception, as you are attempting to call an instance function on a null value.In other words, it is a string; it's just a null string. That is why my suggestion worked; because it filters out any null values. It may not have been apparent because you wouldn't be able to see the null value visibly with this code:
It would be more apparent if you did this instead:
If it is null, you will see "null value" rather than nothing at all.
That makes total sense. Thanks again!
is working on a reply...