Copied to clipboard

Flag this post as spam?

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


  • Damion 59 posts 261 karma points
    1 week ago
    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
    1 week ago
    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 59 posts 261 karma points
    1 week ago
    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 280 posts 1268 karma points
    1 week ago
    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 59 posts 261 karma points
    1 week ago
    Damion
    0

    Nice Shaishav, I'll give that a try

  • Damion 59 posts 261 karma points
    1 week ago
    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 : (

Please Sign in or register to post replies

Write your reply to:

Draft