Why can't I declare an alias with Embedded Content inside a string? I have multiple Embedded Contents on one page, I want to put them inside a string. Though my page just saves as normal, I keep getting an error in my browser.
It's probably because the property is casting to DynamicXml and not a string. You could try making an array of object or DynamicXml instead of strings. Or if you want to force casting to a string, you can use @myNode.GetPropertyValue("subfotos1") instead of @myNode.subfotos1
Thank you for your help! I've already tried both your solutions, but none of them are working (still the same error appears). I'll post all of my script here so everything might get a little bit clearer to you:
@{ var myNode = @Model.NodeById(1080); var quotes = @myNode.quotes; var teaserChar = 90; var sections = ""; var sectionid = "";
As you can see, it's all about the last piece of Razor code '@foreach(dynamic item in @myNode.GetPropertyValue("subfotos1"))'. This keeps throwing me errors. As soon as I replace this with '@foreach(dynamic item in @myNode.subfotos1)' it's all working fine, but then only the contents of 'subfotos1' are loaded. I need to load ALL 'subfotos'-embedded content aliases (goes from subfotos1 to 9).
My final purpose with all of this is to iterate through all 'subfotos#' by using a counting variable (i++). I've tried this trick before and always worked. Until now. :-(
I even removed all code except the last foreach loop I referred to earlier, and this also renders an error for me. So in my opinion, this means all my other code is working just fine (and when I remove this last foreach loop, it actually DOES work fine ;-)
Thank you so much!!! I knew it was just a small word I was missing: 'dynamic[ ]'. Since I knew there had to be more array types than just 'string[ ]', I Google a lot but found none.
Embedded Content in string
Why can't I declare an alias with Embedded Content inside a string?
I have multiple Embedded Contents on one page, I want to put them inside a string.
Though my page just saves as normal, I keep getting an error in my browser.
Here's my code:
As you can see, my Embedded Content aliases are 'subfotos1' and 'subfotos2'.
What's missing here?
Thanks in advance, I really need help with this! :-(
Hi,
It's probably because the property is casting to DynamicXml and not a string. You could try making an array of object or DynamicXml instead of strings. Or if you want to force casting to a string, you can use @myNode.GetPropertyValue("subfotos1") instead of @myNode.subfotos1
HTH,
Tom
Hi Tom,
Thank you for your help! I've already tried both your solutions, but none of them are working (still the same error appears). I'll post all of my script here so everything might get a little bit clearer to you:
As you can see, it's all about the last piece of Razor code '@foreach(dynamic item in @myNode.GetPropertyValue("subfotos1"))'. This keeps throwing me errors.
As soon as I replace this with '@foreach(dynamic item in @myNode.subfotos1)' it's all working fine, but then only the contents of 'subfotos1' are loaded. I need to load ALL 'subfotos'-embedded content aliases (goes from subfotos1 to 9).
My final purpose with all of this is to iterate through all 'subfotos#' by using a counting variable (i++).
I've tried this trick before and always worked. Until now. :-(
By the way, isn't there some sort of debugger so I can see where it goes wrong with my Razor code (just asking)?
I even removed all code except the last foreach loop I referred to earlier, and this also renders an error for me. So in my opinion, this means all my other code is working just fine (and when I remove this last foreach loop, it actually DOES work fine ;-)
Even this handmade compressed script does not work:
@{var [email protected](1080);foreach(dynamic item in @myNode.GetProperty("subfotos1").Value){}}
I even changed the alias to something else, but this also doesn't make a difference.
What could this possible be?!?
Hi Peter,
The compressed script you pasted won't work, because GetPropertyValue returns a string which isn't enumerable in a loop.
So if you just want to loop through the items in an embedded content field, you just do foreach (dynamic item in myNode.subfotos1)
If you want to make an array of several fields as in your original question, I think something like this should work:
-Tom
Hi Tom,
Thank you so much!!! I knew it was just a small word I was missing: 'dynamic[ ]'.
Since I knew there had to be more array types than just 'string[ ]', I Google a lot but found none.
Thanks to you my website is finished!!!
p
is working on a reply...