Copied to clipboard

Flag this post as spam?

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


  • Ulf Möllerström 70 posts 247 karma points
    May 01, 2018 @ 11:36
    Ulf Möllerström
    0

    Dictionary value in ApiController not returning value

    Hello folks!

    I can't get the dictionary value in an api controller...

    Setting the culture doesn't do the trick (either or both), and only an empty string is returned.

    Thread.CurrentThread.CurrentCulture = new CultureInfo(model.Culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(model.Culture);
    var productSelectorPageId = Umbraco.GetDictionaryValue("productSelectorPageId");
    

    Any ideas, anyone?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 01, 2018 @ 11:48
    Dave Woestenborghs
    0

    Hi Ulf,

    A few questions :

    • what is the value of model.culture
    • does the dictionary with key productSelectorPageId exist
    • does the dictionary item have a value for the language from model.culture

    Dave

  • Ulf Möllerström 70 posts 247 karma points
    May 01, 2018 @ 15:46
    Ulf Möllerström
    0

    Hi Dave,

    1/ sv-Se

    2/ yes (in back office atleast)

    3/ yes, the node id for the options to select

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 02, 2018 @ 05:51
    Dave Woestenborghs
    0

    Hi Ulf,

    Can you post some more code from your controller ?

    Because we are using the same approach and that works for us.

    Dave

  • Ulf Möllerström 70 posts 247 karma points
    May 02, 2018 @ 09:00
    Ulf Möllerström
    0

    Sorry, but no, there aren't any more code right now... (Except the method declaration and a try-catch.)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 02, 2018 @ 09:01
    Dave Woestenborghs
    0

    Hi Ulf,

    Probably need the full code to see what's going on.

    Dave

  • Ulf Möllerström 70 posts 247 karma points
    May 02, 2018 @ 09:11
    Ulf Möllerström
    0
    public JsonResult<IEnumerable<CalculationResult>> CalculateProduct(ValuesModel model)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(model.Culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(model.Culture);
    
            var productSelectorPageId = Umbraco.CultureDictionary["productSelectorPageId"];
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    
        return null;
    }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 02, 2018 @ 09:28
    Dave Woestenborghs
    1

    Hi Ulf,

    Are you inheriting from UmbracoApiController ?

    If so can you try this

    this.Umbraco.GetDictionaryValue("productSelectorPageId")
    

    Dave

  • Ulf Möllerström 70 posts 247 karma points
    May 04, 2018 @ 06:38
    Ulf Möllerström
    0

    Dave,

    Yes, UmbracoApiController

    I've tested both of these, and on a page they both return the value, but in the controller they also return an empty string...

    pageCulture = Umbraco.GetDictionaryValue("page_culture");
    pageCulture = Umbraco.CultureDictionary["page_culture"];
    

    Had to make a work-around in my code, so this is now for educational purposes.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    May 01, 2018 @ 16:27
    Ismail Mayat
    0

    Ulf,

    I had similar issue with umbraco v6 site but I was doing some ajax with a surface controller. I fixed it by having a method that set the culture:

    protected void SetCultureForAjax()
        {
            //we lose culture and dictionary wont get labels therefore set it on the thread
            string code = _umbracoService.GetCultureCode();
            //Get the culture info of the language code
            CultureInfo culture = CultureInfo.CreateSpecificCulture(code);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
    

    Although looking at your code you are doing a similar thing to me.

Please Sign in or register to post replies

Write your reply to:

Draft