Copied to clipboard

Flag this post as spam?

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


  • Thomas Stock 40 posts 70 karma points
    Feb 07, 2012 @ 11:09
    Thomas Stock
    0

    Most elegant way to checking a bool property

    Content Tree:

    root
    > VariationRoot
    >> en-US
    >> en-UK

    VariationRoot's Document Type has a isEnabledLanguage true/false property.

    In the VariationRoot's Template I do this:

        // First, collect the available languages in the site
        var availableLanguages = Model.Children().ToList()
            .Where(c => c.Field<string>("isEnabledLanguage") == "True")
            .Select(c => c.UrlName.ToLower());

     

    I was hoping to be able to skip the == "True" but I don't find any documentation about a more elegant way.

    What is a better, cleaner way to do this check?

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 07, 2012 @ 11:31
    Dan Diplo
    0

    Which version of Umbraco are you using? Is this 5?

  • Thomas Stock 40 posts 70 karma points
    Feb 07, 2012 @ 11:36
    Thomas Stock
    0

    I'm sorry, this is indeed Umbraco 5.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 07, 2012 @ 12:17
    Dan Diplo
    0

    In theory you should be able to do:

    var availableLanguages = Model.Children().ToList()
            .Where(c => c.Field<bool>("isEnabledLanguage"))
            .Select(c => c.UrlName.ToLower());

    However, this won't work as True/False fields save the string "True" for true values, but save an empty string for false values. And you can't convert an empty string to a boolean value. I suspect this is because of the way ASP.NET MVC stores checkbox values. But I think it's something that should be addressed by the core team, as you really want to be able to treat True/False values as true booleans. 

  • 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