Copied to clipboard

Flag this post as spam?

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


  • Thomas 319 posts 606 karma points c-trib
    May 31, 2018 @ 11:01
    Thomas
    0

    Nested Content GetPropertyValue

    Hey,

    I'm trying to output some content from my nested content item.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web.Models
    
    var anchors = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("anchor");
    <ul>
        foreach(var anchor in anchors) {
            <li><a href="#">@anchor.GetPropertyValue<String>("anchor")))</a></li>
        }
    </ul>
    

    But i'm getting an error saying that the render model doesn't has "GetPropertyValue" ?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 31, 2018 @ 11:08
    Paul Seal
    0

    Hi Thomas It was great to meet you in person at CodeGarden.

    I think you might be missing this reference

    @using Umbraco.Web
    

    Try adding that at the top.

    Cheers

    Paul

  • Thomas 319 posts 606 karma points c-trib
    May 31, 2018 @ 11:13
    Thomas
    0

    Hi Paul.

    Likewise ! :), thanks for the snaps shot.

    I tried to add that, but it's not changing anything.

    // Thomas

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    May 31, 2018 @ 11:14
    Nik
    101

    Hey Thomas,

    The issue is the point at which you are calling GetPropertyValue.

    Based on the code above, your view is inheriting from UmbracoTemplatePage.

    So, in this scenario, you need to change your third line from:

    var anchors = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("anchor");
    

    to

    var anchors = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("anchor");
    

    Alternatively, you can change your 1st line from:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    to

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    UmbracoTemplatePage wraps your actual Model in a Render Model, so it's exposed via the Model.Content property, where as UmbracoViewPage doesn't.

    It's becoming considered best practice to use UmbracoViewPage instead of UmbracoTemplatePage so I'd switch to that if I were you :-)

    Thanks,

    Nik

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 31, 2018 @ 11:20
    Paul Seal
    2

    Well spotted @nik

    @thomas I used Nested Content in my starter kit for the Main Carousel.

    I put the carousel in a partial view. Here is a link to that partial view, maybe it will help with any other things which come up.

    https://github.com/prjseal/CodeShareUmbracoStarterKit/blob/master/src/CSUSK.Web/Views/Partials/Content/_MainCarousel.cshtml

    Cheers

    Paul

  • Thomas 319 posts 606 karma points c-trib
    May 31, 2018 @ 11:38
    Thomas
    0

    Hey, Thanks guys.

    I have tried this. But are still getting the same issue.

    @inherits UmbracoViewPage
    @using Umbraco.Web.Models
    var anchors = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("anchor");
    
    <ul>
    foreach(var item in anchors) {
      <li><a href="">@item.GetPropertyValue<string>("anchor")</a></li>
        }
    </ul>
    

    I also tried.

     @inherits UmbracoViewPage
     @using Umbraco.Web.Models
     IEnumerable<IPublishedContent> anchors = Umbraco.AssignedContentItem.GetPropertyValue<IEnumerable<IPublishedContent>>("anchor");
    <ul>
    foreach (IPublishedContent item in anchors ) {
        item.GetPropertyValue<string>("anchor")
    }
    

    Still same issue.. ?

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    May 31, 2018 @ 11:55
    Nik
    0

    Hi Thomas,

    That seems odd. Both of those fundamentally look like they should work.

    Are you sure the error is still being thrown in the same view and not now being thrown in a different one?

    The top option is the one that I would go for rather than the second one.

    Nik

  • Thomas 319 posts 606 karma points c-trib
    May 31, 2018 @ 11:59
    Thomas
    0

    I'm creating an grid item with the Doc type grid editor. Does that make any changes ?

Please Sign in or register to post replies

Write your reply to:

Draft