Copied to clipboard

Flag this post as spam?

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


  • Christina 127 posts 390 karma points notactivated
    Jun 17, 2018 @ 11:01
    Christina
    0

    var page = Umbraco.TypedContent(nodeId); returning Object reference not set to an instance of an object.

    Hi Im using a contentpicker to choose a page I get the id but when i im converting to IPublished content
    Object reference not set to an instance of an object. Many thanks Christina

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Activities>
        @using ContentModels = Umbraco.Web.PublishedContentModels;
    
        @using Umbraco.Web;
    
    @{ var nodeId = Model.GetPropertyValue("nodeId"); }
    
        @if (nodeId != null)
        {
    
            var page = Umbraco.TypedContent(nodeId);
    
            foreach (var child in page.Children)
            {
                <a href="@child.Url">@child.Name</a>
            }
        }
    }
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 17, 2018 @ 11:51
    Jan Skovgaard
    100

    Hi Christina

    At a first glance I think your example looks right but I'm wondering if you perhaps need to cast the nodeId to be an int. Could you please try writing it like my example below?

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Activities>
        @using ContentModels = Umbraco.Web.PublishedContentModels;
    
        @using Umbraco.Web;
    
    @{ 
      @* Cast the nodeId to an int here *@ 
        var nodeId = Model.GetPropertyValue<int>("nodeId"); 
       }
    
        @if (nodeId != null)
        {
    
            var page = Umbraco.TypedContent(nodeId);
    
            foreach (var child in page.Children)
            {
                <a href="@child.Url">@child.Name</a>
            }
        }
    }
    

    Does this help?

    /Jan

  • Christina 127 posts 390 karma points notactivated
    Jun 18, 2018 @ 06:12
    Christina
    0

    Hi Jan Many thanks, yes it worked. /Christina

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 18, 2018 @ 06:38
    Jan Skovgaard
    0

    Happy to hear Christina - Happy umbracoing! :-)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft