How to render n-th element or first element in nested content?
I want to just the render first element of nested content.
I managed to get the values with the code below, is there any other practical way?
var infos = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("contactInfos");
var i=0;
foreach(var info in infos){
if(i==0){
@Umbraco.Field(info, "email")
}
i++;
}
I used your suggested solution on previous projects. However it doesn't work on Umbraco 7.5.4. and 7.5.3
I get the following error.
'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'FirstOrDefault'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'FirstOrDefault'
I'm not sure where the System.Collections.Generic.List part is coming from... is that in your code? The example above uses IEnumerable<IPublishedContent>.
I'm not totally sure what is going on... you say it did previously work, then you upgraded and it no longer worked?
The actual error "does not contain a definition for 'FirstOrDefault'" usually means that the Razor view can't find the namespace for an extension method... so you could try adding this to the very top of your Razor script...
@using System.Linq
Hopefully that may work?
Otherwise, from looking at your last reply, my only thought would be that the chaining of the extension methods may have a null reference at some point - it could be the .First() straight after the Descendants?
You may need to break down the single line statement, so it can be debugged better?
e.g.
var root = CurrentPage.AncestorOrSelf(1);
var contactPages = root.Descendants("contactPage");
var contactPage = contactPages .First();
var offices = contactPage.GetPropertyValue<IEnumerable<IPublishedContent>>("offices");
var office = offices.FirstOrDefault();
I know it's more lines of code... but it will help you identify where the exact problem is.
How to render n-th element or first element in nested content?
I want to just the render first element of nested content.
I managed to get the values with the code below, is there any other practical way?
Thanks!
Hi Osman,
You could try this...
I hope this helps?
Cheers,
- Lee
Hi again Lee,
I used your suggested solution on previous projects. However it doesn't work on Umbraco 7.5.4. and 7.5.3
I get the following error.
Any suggestions? Thanks in advance...
Hi Osman,
That's pretty strange.
I'm not sure where the
System.Collections.Generic.List
part is coming from... is that in your code? The example above usesIEnumerable<IPublishedContent>
.Cheers,
- Lee
Hi Lee,
What I'm trying to do is get the contact information of the first office (nested content data type) to display on footer area.
I use the code:
What am i doing wrong?
Hi Osman,
I'm not totally sure what is going on... you say it did previously work, then you upgraded and it no longer worked?
The actual error "
does not contain a definition for 'FirstOrDefault'
" usually means that the Razor view can't find the namespace for an extension method... so you could try adding this to the very top of your Razor script...Hopefully that may work?
Otherwise, from looking at your last reply, my only thought would be that the chaining of the extension methods may have a
null
reference at some point - it could be the.First()
straight after theDescendants
?You may need to break down the single line statement, so it can be debugged better?
e.g.
I know it's more lines of code... but it will help you identify where the exact problem is.
Cheers,
- Lee
Hi Lee,
The client don't want head office contact info in the footer any more :)
But i couldn't find where the error comes from.
If i loop through offices, i get the values without any problems.
@using System.Linq didn't help either.
Any ways thanks for helping. Best regards...
Great!
Thanks for the tip. Is there a short method to get n-th element?
Cheers :)
Hi Osman,
The easiest way would be to use the
.Skip
LINQ method.e.g. set the
idx
variable as the number before the one you want, so if it's the 4th, then set to 3.Hope this helps?
Cheers,
- Lee
Works :)
Thank you very much Lee.
Hi Osman
As alternative you can also use ElementAtOrDefault to get a specific element in the collection:
https://msdn.microsoft.com/library/bb494386.aspx
https://www.dotnetperls.com/elementatordefault
I am not sure it that is slower than the suggestion Lee mentioned.
/Bjarne
Hi Bjarne,
I get an error:
'System.Collections.Generic.List
That portion of code is not needed on the project. I'll try it on next one :)
Thanks for helping...
is working on a reply...