Copied to clipboard

Flag this post as spam?

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


  • Dan Smith 13 posts 93 karma points
    Aug 11, 2016 @ 22:47
    Dan Smith
    0

    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.

  • Dan Smith 13 posts 93 karma points
    Aug 12, 2016 @ 14:56
    Dan Smith
    0

    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.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Aug 12, 2016 @ 15:20
    Nicholas Westby
    100

    Try this:

    selection = selection.Select(x => new
    {
        Value = x.GetPropertyValue<string>("projectType"),
        Content = x
    })
    .Where(x => !string.IsNullOrWhiteSpace(x.Value))
    .Where(x => x.Value.Split(',').Any(y => filterProjectTypes.Contains(y)))
    .Select(x => x.Content)
    .ToList();
    
  • Dan Smith 13 posts 93 karma points
    Aug 12, 2016 @ 15:31
    Dan Smith
    0

    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.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Aug 12, 2016 @ 16:41
    Nicholas Westby
    0

    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.)?

  • Dan Smith 13 posts 93 karma points
    Aug 12, 2016 @ 17:11
    Dan Smith
    0

    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.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Aug 12, 2016 @ 17:18
    Nicholas Westby
    0

    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.

  • Dan Smith 13 posts 93 karma points
    Aug 12, 2016 @ 17:27
    Dan Smith
    0

    That makes total sense. Thanks again!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies