Copied to clipboard

Flag this post as spam?

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


  • George Phillipson 108 posts 287 karma points
    Apr 21, 2019 @ 11:04
    George Phillipson
    1

    Model.Cultures returns null

    Trying to set up a language switcher, using the following code which I have seen on a number of V8 blog posts

    @foreach (var (culture, infos) in Model.Cultures)
    {
    <a href="@Model.GetUrl(culture)">@Model.Value("language",culture)</a>
    }
    

    Problem is, it returns an Object reference not set to an instance of an object.

    The cultures work as I can switch between languages on the test site I'm playing about with.

    Anyone else came across this problem, or know how to fix it

    Just an update to this, the following code from V7 still works OK

    @{
        var homePage = Umbraco.ContentAtRoot().FirstOrDefault();
        string redirectToCulture = homePage.Value<IPublishedContent>("redirectToCulture",culture:"en-GB").UrlAbsolute();
    
        foreach (var lang in Services.LocalizationService.GetAllLanguages())
        {
            <a href="@($"{redirectToCulture}?lang={lang.IsoCode}")">@lang.CultureName</a>
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft