Copied to clipboard

Flag this post as spam?

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


  • mizzle 90 posts 325 karma points
    Dec 05, 2019 @ 20:44
    mizzle
    0

    Unable to retrieve field values of Nested Content in separate Partials (Umbraco 8)

    I had an Umbraco 7 setup that allowed me to render Nested Content with a foreach loop, pulling in separate partial files to render the information, mimicking the macros/"widget" customization on older versions of Umbraco. On a View page, I would have the following:

    var items = Model.Value<IEnumerable<IPublishedElement>>("widgetsPicker");
    
    if (items != null)
    {
        foreach (var item in items)
        {
            String viewPath = null;
    
            viewPath = "~/Views/Partials/Widgets/" + item.ContentType.Alias + ".cshtml";
            try
            {
                @Html.Partial(viewPath, item);
            }
            catch
            {
                <p> Error in widget "@viewPath.ToString()".</p>
            }
        }
    }
    

    but this code doesn't appear to translate to Umbraco 8. The @Html.Partial(viewPath, item); section throws an error because of the 'item'. I can remove 'item' from the code and it pulls the Partial in, but anything within that partial tries to read fields on the main page instead of the fields of the Nested Content element, so it renders nothing. There doesn't seem to be a way to link the Nested Content to the Partial code.

    Is there something I can do to get Nested Content working how I want?

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Dec 06, 2019 @ 05:33
    Matt Barlow | jacker.io
    100

    Sounds like your views are expecting a different model to the one you are passing in.

    Should have this in your partial view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    See this post for more info: https://our.umbraco.com/forum/templates-partial-views-and-macros/97248-nested-content-in-separate-partials

  • mizzle 90 posts 325 karma points
    Dec 06, 2019 @ 14:27
    mizzle
    0

    I think that might have fixed the issue. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft