Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 343 karma points
    Dec 13, 2019 @ 10:18
    Tom Cowling
    0

    Struggling to test a boolean value

    I have spun up a new site and decided to use Umbraco 8. Previously I've used 7 and had a few partial views to do menus, breadcrumbs etc. I've been trying various things over the past couple of days and can't seem to get anywhere with this one.

    In a previous Umbraco 7 I had a partial view that worked fine to produce a top menu. The first problem I hit was that it didn't like me using the following namespace:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    So I thought fine, I just go with a template one and notice it uses:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using Umbraco.Web
    

    I carried on customising the template an it works fine. Now I want to test if a simple tickbox has a value of true or not to be included in the array that comes as a default with that template. In my Umbraco 7 partial view, I have the following .Where("showInNavigation == True"), which no longer works. I tried including the @using Umbraco.Core.Models. The closet I have got so far is the following:

    @{
        var selection = Model.Root()
        .Children
        .Where(x => (x.showInNavigation == "True"))
        .Where(x => x.IsVisible())
        .ToArray();
    }
    

    Sadly, this produces a CS1061: 'IPublishedContent' does not contain a definition for 'showInNavigation' and no accessible extension method 'showInNavigation' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)' (are you missing an assembly reference?) error. If someone could please shed some light onto what I'm doing wrong, that would be really great.

    Thanks,

    Tom

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Dec 13, 2019 @ 10:59
    Kevin Jump
    100

    Hi Tom,

    In Umbraco 8 you can no longer use Dynamic property values (so that's where it know what x.ShowInNavigation is.

    You can either use Models Builder which will provide you, models, with the properties on, or you can call the value by name in a Value("propertyName") call.

    e.g

    var selection = Model.Root().Children.Where(x => x.Value<bool>("showInNavigation") == true); 
    

    assuming ShowInNavigation is a true/false property on your items

  • Tom Cowling 144 posts 343 karma points
    Dec 13, 2019 @ 11:17
    Tom Cowling
    0

    Perfect. Thanks for clarifying.

  • Garðar Þorsteinsson 118 posts 564 karma points
    Dec 13, 2019 @ 11:26
    Garðar Þorsteinsson
    0

    Just to add to Kevins response.

    Get All True boolean

    var selection = Model.Root().Children.Where(x => x.Value<bool>("showInNavigation")); 
    

    Get All False boolean

    var selection = Model.Root().Children.Where(x => !x.Value<bool>("showInNavigation"));
    
Please Sign in or register to post replies

Write your reply to:

Draft