Copied to clipboard

Flag this post as spam?

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


  • Genc Kastrati 86 posts 401 karma points
    Nov 01, 2019 @ 17:42
    Genc Kastrati
    0

    Programmatically getting Dictionary value - formerly GetDictionaryValue()

    Hi,

    Under Umbraco 7 I used to have a method that would get a dictionary label and return the correct value. It used to be done with UmbracoHelper. How can this be done in Umbraco 8 now?

    This is the old method:

    public static string GetLabel(string label)
    {
        return umbracoHelper.GetDictionaryValue(label);
    }
    

    I have seen a number of other posts recommending using Umbraco. but this doesn't seem to work as I see no methods. Thank you for any help! Genc

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Nov 01, 2019 @ 17:56
    Nik
    1

    Hey Genc,

    In my views in V8 I'm doing Umbraco.GetDictionaryValue("DictionaryItemAlias","FallBackValue")

    In a view Umbraco is an exposed version of an UmbracoHelper much in the same way you are using it in your static method so it should still be possible.

    Some thing to be aware of, ideally you shouldn't be creating your own instance of the Umbraco helper, you could consider creating an Extension method though.

    E.g.

    public static string GetLabel(this UmbracoHelper helper, string label)
    {
          return helper.GetDictionaryValue(label);
    }
    

    This would rely on you having an instance of an UmbracoHelper which, hopefully, you aren't creating an instance of but using an already existing instance either from your view, your controller, or injected via dependency injection into custom code.

    Nik

  • Genc Kastrati 86 posts 401 karma points
    Nov 07, 2019 @ 11:00
    Genc Kastrati
    0

    Thank you so much Nik. Made progress. I have one of those precompiled versions so had to change stuff all over the place. Looks like it is on the right track though. Thank you!

  • BJ Patel 80 posts 206 karma points
    Aug 24, 2021 @ 11:01
    BJ Patel
    0

    If inheriting from UmbracoApiController / SurfaceController

    Following may work :

    this.Umbraco.GetDictionaryValue("label")
    

    BJ

Please Sign in or register to post replies

Write your reply to:

Draft