Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Sep 12, 2019 @ 14:02
    Dibs
    0

    rendering nested content properties

    Dear Umbraco Team

    I have a nested content data type making use of a document type with 2 x textbox, 1 x media picker and 1 multi Url picker. I can render the text and media properties as normal but not the mutli Url Picker with the following code

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        var items = Model.Value<IEnumerable<IPublishedElement>>("homeBannerSettings");
    }
    @if (items.Any())
    {
        foreach (var item in items)
        {
            var bannerTitle = item.Value("homeBannerTitle");
            var bannerBlurb = item.Value("homeBannerBlurb");
            var bannerLink = item.Value("homeBannerLink");
            <div class="jumbotron rounded-0 text-white">
                <h1 class="display-4">@bannerTitle</h1>
                <p class="lead">@bannerBlurb</p>
                <p class="lead">
                    <a class="btn btn-primary btn-lg" href="@bannerLink" role="button">Learn more</a>
                </p>
            </div>
        }
    }
    

    The above code displays a 404 error with

    System.Collections.Generic.List%601[Umbraco.Web.Models.Link]

    When nested content link property is clicked. To resolve this error i used the following code

    @using Umbraco.Web.Models
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        var items = Model.Value<IEnumerable<IPublishedElement>>("homeBannerSettings");
    }
    @if (items.Any())
    {
        foreach (var item in items)
        {
            var bannerTitle = item.Value<string>("homeBannerTitle");
            var bannerBlurb = item.Value<string>("homeBannerBlurb");
            var bannerLink = item.Value<Link>("homeBannerLink");
            <div class="jumbotron rounded-0 text-white">
                <h1 class="display-4">@bannerTitle</h1>
                <p class="lead">@bannerBlurb</p>
                <p class="lead">
                    <a class="btn btn-primary btn-lg" href="@bannerLink.Url" role="button">Learn more</a>
                </p>
            </div>
        }
    }
    

    Now when i get a YSOD

    Object reference not set to an instance of an object.

    for nested content text properties.

    How do you render nested content properties in Umbraco 8 correctly ?

    Thanks Dibs

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 12, 2019 @ 18:06
    Shaishav Karnani from digitallymedia.com
    0

    Hi,

    You can either convert it to strongly type model as that will help with better error handling.

    PS: I don't know alias of your Nested Document Type so have named it NestedDocumentType in below code.

        @using Umbraco.Web.Models
        @inherits Umbraco.Web.Mvc.UmbracoViewPage
        @{
            var items = Model.Value<IEnumerable<IPublishedElement>>("homeBannerSettings");
        }
        @if (items.Any())
        {
            foreach (var _item in items)
            {
        var item = new NestedDocumentType(_item);
    
                <div class="jumbotron rounded-0 text-white">
    @if (!string.IsNullOrEmpty(item.HomeBannerTitle)) {
                    <h1 class="display-4">@item.HomeBannerTitle</h1>
    }
    @if (!string.IsNullOrEmpty(item.HomeBannerBlurb)) {
                    <p class="lead">@item.HomeBannerBlurb</p>
    }
    @if (item.HomeBannerLink != null) {
                    <p class="lead">
                        <a class="btn btn-primary btn-lg" href="@item.HomeBannerLink.Url" role="button">Learn more</a>
                    </p>
    }
                </div>
            }
        }
    

    Please review and let me know if this helps or you need further assistance.

    Cheers,

    Shaishav

  • Dibs 202 posts 991 karma points
    Sep 16, 2019 @ 12:15
    Dibs
    100

    Thanks Shaishav

    for your reply.

    var bannerLink = item.Value<Link>("homeBannerLink");
    

    no works as expected, i may have had cache issue with local dev environment

    Dibs : )

Please Sign in or register to post replies

Write your reply to:

Draft