Copied to clipboard

Flag this post as spam?

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


  • Sharmarke Hujale 103 posts 346 karma points
    Dec 20, 2015 @ 19:50
    Sharmarke Hujale
    0

    Cannot perform runtime binding on a null reference

    Hi guys,

    I have the following code which is returning an error:

    Cannot perform runtime binding on a null reference
    

    Can anyone help me with this?

     @foreach (var item in CurrentPage.Parent.Children.Where("Visible").OrderBy("CreateDate desc").Take(5))
            {
    
                <div class="col-md-4 col-sm-12">
                    <article class="portfolio-item">
                        <div class="portfolio-thumbnail">
                            <a style="background-image: @item.GetPropertyValue("projectImage")" href="@item.Url">
                                <div style="@item.GetPropertyValue("hoverColor")" class="overlay-img">
                                    <span class="overlay-ico">
                                        <i class="fa fa-link fa-5"></i>
                                    </span>
                                </div>
                            </a>
                        </div>
                        <span class="separator-small"></span>
                        <h3 class="portfolio-title">@item.Name</h3>
                    </article>
                </div>
            }
    

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2015 @ 19:58
    Dennis Aaen
    0

    Hi Sharmarke,

    It´s it possible for you to share your content tree, and also explain on which page in the content tree that you are trying to execute this Razor code.

    Then I think it´s easier to help you find the right solution, or why your code throws this null reference error.

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 20, 2015 @ 20:03
    Sharmarke Hujale
    0

    Hi Dennis,

    Of course - here it is:

    enter image description here

    I'm trying to execute my razor in my Home view page to show top 3 of my last projects.

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2015 @ 20:11
    Dennis Aaen
    0

    Hi Sharmarke,

    I assume that you projects is child nodes of the project node. if so, then you should be able to do something like this.

            @{      
    
                var selection = CurrentPage.AncestorOfSelf(1).Descendants("Project").FirstOrDefault();
            }
    
     @foreach (var item in selection.Children.Where("Visible").OrderBy("CreateDate desc").Take(3))
            {
    
                <div class="col-md-4 col-sm-12">
                    <article class="portfolio-item">
                        <div class="portfolio-thumbnail">
                            <a style="background-image: @item.GetPropertyValue("projectImage")" href="@item.Url">
                                <div style="@item.GetPropertyValue("hoverColor")" class="overlay-img">
                                    <span class="overlay-ico">
                                        <i class="fa fa-link fa-5"></i>
                                    </span>
                                </div>
                            </a>
                        </div>
                        <span class="separator-small"></span>
                        <h3 class="portfolio-title">@item.Name</h3>
                    </article>
                </div>
            }
    

    Remember to change the project in the Descendants() so it match your alias for the project page.

    Hope this helps,

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 20, 2015 @ 20:28
    Sharmarke Hujale
    0

    Hi Dennis,

    I changed it to this:

    var selection = CurrentPage.AncestorOfSelf(1).Descendants("Portfolio").FirstOrDefault();
    

    Now I don't have the 'null' anymore, but it doens't show the three latest projects

    My child nodes are named 'Post',

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2015 @ 21:55
    Dennis Aaen
    0

    Hi Sharmarke,

    Okay if you try run this piece of code below did you then get the name of the name of the portfolio node

    @{          
          var selection = CurrentPage.AncestorOfSelf(1).Descendants("Portfolio").FirstOrDefault();
     }
    
    @selection.Name
    

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 20, 2015 @ 22:04
    Sharmarke Hujale
    0

    Hi Dennis

    I've tried that - and I still get nothing

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2015 @ 22:41
    Dennis Aaen
    0

    Hi Sharmake,

    Okay then I think that the alias of your portfolio page could be wrong.

    Be sure that you are adding the right document type alias,

    In the image blow the document type alias is umbTextPage

    enter image description here

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 20, 2015 @ 22:57
    Sharmarke Hujale
    0

    Hi Dennis

    Should I use my Portfolio page's alias or my child node's alias 'Post'?

    enter image description here

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2015 @ 23:19
    Dennis Aaen
    0

    Hi Sharmake,

    You should use the document type alias of the Portfolio.

    What the the code below does, is that we go up to the root node of your website, then you search down your content tree until you find a page that uses the umbTextPage document type, and then you take the first.

    @{          
      var selection = CurrentPage.AncestorOrSelf(1).Descendants("umbTextPage").FirstOrDefault();
    }
    
    @selection.Name
    

    What you need to do then is to write a foreach loop so we can get the children of this page, in your case the posts. And in this foreach loop you can add the Where, OrderBy and the Take methods.

    Hope this make sense.

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 22, 2015 @ 09:40
    Sharmarke Hujale
    0

    Hi Dennis

    Sorry for the late reply,

    I understand clearly of what you're saying, and I've tried it out again,

    But it still shows nothing.

    Here's my code:

    var selection = CurrentPage.AncestorOfSelf(1).Descendants("Portfolio").FirstOrDefault();
    
    
     @foreach (var item in selection.Children.Where("Visible").OrderBy("CreateDate desc").Take(3))
            {
                <div class="col-md-4 col-sm-12">
                    <article class="portfolio-item">
                        <div class="portfolio-thumbnail">
                            <a style="background-image: @item.GetPropertyValue("projectImage")" href="@item.Url">
                                <div style="@item.GetPropertyValue("hoverColor")" class="overlay-img">
                                    <span class="overlay-ico">
                                        <i class="fa fa-link fa-5"></i>
                                    </span>
                                </div>
                            </a>
                        </div>
                        <span class="separator-small"></span>
                        <h3 class="portfolio-title">@item.Name</h3>
                    </article>
                </div>
            }
    

    I go to the root of my website, finds the alias of portfolio and takes it children. But nothing seems working at all..

    /Sharmarke

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 22, 2015 @ 09:57
    Sharmarke Hujale
    0

    Hi again, Dennis

    I found something interesting,

    I changed this:

    var selection = CurrentPage.AncestorOfSelf(1).Descendants("portfolioPage").FirstOrDefault();
    

    To this:

    var selection = Model.Content.AncestorOrSelf(1).Descendants("portfolioPage").FirstOrDefault();
    

    And now it's displaying my latest three projects.. But why isn't it working, when I'm using the 'CurrentPage'?

    /Sharmarke

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 22, 2015 @ 10:11
    Dennis Aaen
    100

    Hi Sharmarke

    Ah, great. I can give you an explanation of why you don´t get any data out with the code below.

    var selection = CurrentPage.AncestorOfSelf(1).Descendants("portfolioPage").FirstOrDefault();
    

    In Umbraco you have the option to work with two different types of Razor. You can work with dynamic Razor where current page is @CurrentPage or you can work with strongly typed Razor where current page is Model.Content.

    You can see the difference on the Razor implementations on these cheatsheets https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets is also for Umbraco 7, and it just pdf files with overview over the methods that you have.

    Also try to see this documentation, https://our.umbraco.org/documentation/reference/templating/mvc/views and https://our.umbraco.org/documentation/reference/templating/mvc/partial-views

    Hope this make sense and helps,

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 22, 2015 @ 10:22
    Sharmarke Hujale
    0

    Hi Dennis

    Thanks again for your help and time - I appreciated it.

    Take care,

    /Sharmarke

Please Sign in or register to post replies

Write your reply to:

Draft