Copied to clipboard

Flag this post as spam?

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


  • EKohl 6 posts 86 karma points
    May 21, 2019 @ 17:11
    EKohl
    0

    Fallback language in razor query

    Hi Everyone, I'm new to Umbraco and Razor. I'm trying to generate a list of pages on a parent page. On the French version of the page, I want it to fallback to the default language (English) for any item in the list that doesn't have an equivalent published French page. This allows us to publish release notes in English and show the English details there until the French page is published a few days later. The following code works just fine in English:

        var releases = Model.Root().Children?
            .OfType<News>()?.First()?
            .Children()
            .Select(x => (NewsArticle)x)
            .Where(x => x.IsPublished() && x.IsVisible() && x.VersionNumber != null && x.HasValue("Product"))
            .OrderByDescending(x => x.ReleaseDate)
            .Take(5);
        var product1Releases = releases.Where(x => x.Value<string>("Product") == "Product1");
        var product2Releases = releases.Where(x => x.Value<string>("Product") == "Product2");
    

    Next, I have the following code, which fails when viewing the page in French as there is no French version of a Product2 page:

        <div class="col-lg-4 col-md-4 col-sm-6 mb-30">
            <div class="product-card mx-auto">
                <a class="product-thumb" href="#">
                    <img src="/img/download-product2.png" alt="Product Thumbnail" />
                </a>
                <div class="product-card-body">
                    @{ var product2Current = product2Releases.First();}
                    <span class="product-meta mt-0 pt-0">
                        @product2Current.UpdateDate.ToShortDateString()
                    </span>
                    <h2 class="display-4 font-weight-bold text-success">
                        @product2Current.Value("Product") 
                    </h2>
                    <span class="product-price">
                        <a href="@product2Current.Url">Release Notes</a>
                    </span>
                </div>
                <div class="product-buttons-wrap">
                    <div class="product-buttons">
                        <a class="btn btn-style-5 btn-success" href="@product2Current.Url">
                            Download Now
                        </a>
                    </div>
                </div>
            </div>
        </div>
     <div class="accordion accordion-style-3" id="product2-versions">
                @foreach (var product2Release in product2Releases)
                {
                    <div class="card">
                        <div class="card-header">
                            <h6>
                                <a class="collapsed" href="#@product2Release.UrlSegment" data-toggle="collapse">
                                    @product2Release.Product Version @product2Release.Value("versionNumber")
                                </a>
                            </h6>
                        </div>
                        <div class="collapse" id="@product2Release.UrlSegment" data-parent="#product2-versions">
                            <div class="card-body">
                                <div>
                                    <p class="text-muted">@product2Release.ReleaseDate.ToShortDateString()</p>
                                    <p>@product2Release.SeoMetaDescription</p>
                                    <p><a href="@product2Release.Url">Release Notes</a></p>
                                    <a class="btn btn-style-4 btn-secondary" href="@product2Release.SetupLink"><i class="fe-icon-download"></i> Download Now</a>
    
                                </div>
                            </div>
                        </div>
                    </div>
                }
            </div>
    

    It fails specifically on the line @{ var product2Current = product2Releases.First();}

    I've read all that I can find about the fallback languages. This was helpful: https://our.umbraco.com/Documentation/Getting-Started/Design/Rendering-Content/#using-fall-back-methods

    The problem I'm having is that all the examples explain how to specify a fallback value for a property, but I'm trying to get it to include pages from the fallback language in the list pages. I suspect something is missing from how I am defining the variables, but I'm not sure what.

Please Sign in or register to post replies

Write your reply to:

Draft