@foreach (var child in CurrentPage.Children) {
switch (child.GetDocumentType){ // Obviously "GetDocumentType" doesn't exsist
case "Type1":
// Do magic stuff
break;
case "Type2":
// Even more magic here
break;
...
...
}
}
Apparently, you can't just type child.DocumentType, I've searched the forums and Google without any luck.
By my own experiments (I'm fairly new to Umbraco) I found out that one way of getting the document type is to do:
@foreach (var child in CurrentPage.Children){
Node n = new Node(child.Id);
switch(n.NodeTypeAlias){
...
}
}
So my question is: Is this the best way to go to get the document type of a child?
@foreach (var child in CurrentPage.Children) {
switch (child.DocumentTypeAlias){
case "Type1":
// Do magic stuff
break;
case "Type2":
// Even more magic here
break;
...
...
}
}
because CurrentPage is dynamic type whereas the Model.Content is not.
So the type of child.DocumentTypeAlias is unknown until runtime but the Switch statement needs to know the type before the runtime that's why you where getting error.
in the 2nd foreach loop the child is not dynamic any more and Child.DocumentTypeAlias returns string
Get Document Type of Child
I want to do is something like this:
Apparently, you can't just type
child.DocumentType
, I've searched the forums and Google without any luck.By my own experiments (I'm fairly new to Umbraco) I found out that one way of getting the document type is to do:
So my question is: Is this the best way to go to get the document type of a child?
I'm trying to avoid doing an
if..else if..else
.Hi Nikola,
you should use this
DocumentTypeAlias
so your code would be like:
Hey Ali!
I tried that before and got a
CS0151: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type
.BUT, I don't know why I didn't try this before, if I typecast it to
string
it works:Thanks for your help
you are welcome Nikola.
I am wondering what version of Umbraco are you using? because in V6 the output of child.DocumentTypeAlias is string.
Thanks
Ali
I'm using 6.1.3.
Why it's not returning a string is beyond me.
I made a function of my
switch
-statement that looks like this:Here the DocumentTypeAlias outputs a string. I probably did something stupid before :)
I noticed this:
alright that makes sense now.
because
CurrentPage
is dynamic type whereas the Model.Content is not.So the type of
child.DocumentTypeAlias
is unknown until runtime but the Switch statement needs to know the type before the runtime that's why you where getting error.in the 2nd foreach loop the child is not dynamic any more and
Child.DocumentTypeAlias
returns stringhope it makes it clear.
Cheers
Ali
That acctually helps out a lot!
Thanks for all your help!
is working on a reply...