Copied to clipboard

Flag this post as spam?

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


  • Damion 96 posts 331 karma points
    Sep 11, 2019 @ 14:06
    Damion
    0

    access dictionary values in partial view with inherited model

    I have a partial view where the model is inherited from a ViewModel instead on IPublishedContent.

    How then can I access Umbraco dictionary values in the partial? If inheriting from IPublishedContent the I just do this obviously

    @Umbraco.GetDictionaryValue("Registration Text", "Reg txt")
    

    I'm unsure how to do this when not inheriting from IPublishedContent.

    thanks

  • Daniel 60 posts 174 karma points
    Sep 11, 2019 @ 15:07
    Daniel
    0

    Hi Damion !

    Do you have the option to simply add another "string" property onto the ViewModel ? Like another property called "RegistrationText" or maybe "DicRegistrationText" or similar? If so you should be able to get it easy on the Model object.

    It's a common MVC strategy to try and get the calls like "Umbraco.GetDictionaryValue" into the Controllers so you keep the Views nice and clean MVC-wise with only simple types, usually this means you can jump into where the ViewModel is created and set this value there.

    If you're not sure where this is done in the solution, try and search for whereever in the solution it says "new [NameOfViewModel]", hopefully that's only one place :)

  • Damion 96 posts 331 karma points
    Sep 11, 2019 @ 15:14
    Damion
    0

    Hi,

    Yes I tried that, I have a test string on the ViewModel and tried to set it as

    model.testTxt = Umbraco.GetDictionaryValue("registration-agree-terms");
    

    on the render form action, but again nothing is returned. I thought using the Umbraco helper here would have taken care of it. It's not one of these weird v8 things is it?

    EDIT

    just noticed that if I place a breakpoint below the line where I try to set testTxt and hover over 'Umbraco', I see this:

    'Umbraco.AssignedContentItem' threw an exception of type 'System.InvalidOperationException'
    
  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 12, 2019 @ 06:24
    Shaishav Karnani from digitallymedia.com
    0

    Hi Damion,

    You can write your own DictionaryService to access it and registers as singleton.

      // Registering IDictionaryService composer  
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class RegisterComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<IDictionaryService, DictionaryService>(Lifetime.Singleton);
            }
        }
    
      // Creating IDictionaryService Class 
    namespace Training.Web.Umbraco.Domain.Dictionary
    {
        public interface IDictionaryService
        {
            string GetDictionaryValue(string key);
            string GetDictionaryValue(string key, string altText);
        }
    
        public class DictionaryService : IDictionaryService
        {
            private readonly ICultureDictionaryFactory _cultureDictionaryFactory;
            private ICultureDictionary _cultureDictionary;
    
            T Ensure<T>(T o) where T : class => o ?? throw new InvalidOperationException("This UmbracoHelper instance has not been initialized.");
    
            private ICultureDictionaryFactory CultureDictionaryFactory => Ensure(_cultureDictionaryFactory);
    
            public ICultureDictionary CultureDictionary => _cultureDictionary
                 ?? (_cultureDictionary = CultureDictionaryFactory.CreateDictionary());
    
            public DictionaryService(ICultureDictionaryFactory cultureDictionary)
            {
                _cultureDictionaryFactory = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
            }
    
            public string GetDictionaryValue(string key)
            {
                return CultureDictionary[key];
            }
    
            public string GetDictionaryValue(string key, string altText)
            {
                var dictionaryValue = GetDictionaryValue(key);
                if (String.IsNullOrWhiteSpace(dictionaryValue))
                {
                    dictionaryValue = altText;
                }
                return dictionaryValue;
            }
    
        }
    }
    

    In your Partial View, now you can do to get the value based on culture:-

    @using Current = Umbraco.Web.Composing.Current;
    IDictionaryService DictionaryService = Current.Factory.GetInstance<IDictionaryService>();
    DictionaryService.GetDictionaryValue("registration-agree-terms");
    
  • Damion 96 posts 331 karma points
    Sep 12, 2019 @ 07:37
    Damion
    0

    Nice Shaishav, I'll give that a try

  • Damion 96 posts 331 karma points
    Sep 12, 2019 @ 08:20
    Damion
    0

    Although it was good to be able to debug through the code in the service it still returned nothing. Not sure where to go with this now : (

  • Nilesh J. Macwan 4 posts 74 karma points
    Apr 08, 2021 @ 07:40
    Nilesh J. Macwan
    0

    I have faced same problem. When i have uploaded dlls into bin folder at that time below error occur on the server. i need to upload again DictionaryService services class . enter image description here

  • Nilesh J. Macwan 4 posts 74 karma points
    Feb 07, 2022 @ 09:52
    Nilesh J. Macwan
    0

    Hello Experts,

    Priority: URGENT.

    I am facing this issue as and when and just by excluding the web.config file and reuploading the DLL files again on the server the issue gets resolved.

    Can anyone help me to understand why this issue is taking place and what is the solutions?

Please Sign in or register to post replies

Write your reply to:

Draft