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.
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.
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.
Cannot perform runtime binding on a null reference
Hi guys,
I have the following code which is returning an error:
Can anyone help me with this?
/Sharmarke
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
Hi Dennis,
Of course - here it is:
I'm trying to execute my razor in my Home view page to show top 3 of my last projects.
/Sharmarke
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.
Remember to change the project in the Descendants() so it match your alias for the project page.
Hope this helps,
/Dennis
Hi Dennis,
I changed it to this:
Now I don't have the 'null' anymore, but it doens't show the three latest projects
My child nodes are named 'Post',
/Sharmarke
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
/Dennis
Hi Dennis
I've tried that - and I still get nothing
/Sharmarke
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
/Dennis
Hi Dennis
Should I use my Portfolio page's alias or my child node's alias 'Post'?
/Sharmarke
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.
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
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:
I go to the root of my website, finds the alias of portfolio and takes it children. But nothing seems working at all..
/Sharmarke
Hi again, Dennis
I found something interesting,
I changed this:
To this:
And now it's displaying my latest three projects.. But why isn't it working, when I'm using the 'CurrentPage'?
/Sharmarke
Hi Sharmarke
Ah, great. I can give you an explanation of why you don´t get any data out with the code below.
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
Hi Dennis
Thanks again for your help and time - I appreciated it.
Take care,
/Sharmarke
is working on a reply...