As you see, I'm trying to have a loop which creates <a href>'s on the overview page for all 9 subpages. The item I've started to make dynamic is the <h5>. When I get a solution for that one, I guess I can figure out the other tags (<p>, <img>) by myself. ;-)
At the top you see how I made the link to the "section_#" parts dynamic as well.
As you can see, I used an array in combination with variable i. But this also doesn't work! When I try '@sub_paginatitel[1]' it all works fine, and when I enter i the file saves correctly. But when I refresh my site after that, I get an error on my site. :-(
I tried every usable loop type (for & while), and both won't work. As soon as I remove the loops and type var i = 1; the var is succesfully included in the array. So what is my loop doing to this variable that it's not usable for the array anymore?!?
When you render this page and view the source, you are probably finding that this line right here isn't actually outputting anything? The reason for that would be because the @{ ... } indicates a code block, not actually an instruction to output anything. To illustrate what I mean, notice the difference between these next two lines:
<h5>@{ "someString" }</h5>
The above code block in the H5 tag is just a string that is not output.
<h5>@something</h5>
However the above will.. So you could try chaning your line to this:
Notice all I did was remove the braces (and the semicolon) that were telling Razor to treat as a code block, not as something to ouput. You were really close and this is just one of those Razor things to keep in mind.
OK, now to my second suggestion about your error when you tried putting this in an array. Remember, your umbraco property names all end with 1 through 9... but when you deal with arrays, they start at zero! So looping through your array would need to go from 0 to 8, not 1 to 9. Although I personally would skip the array and all the manual declarations/mappings you are doing, and spend the effor to try and get the loop working using @myNode.GetProperty.
Good luck!
P.S., another way I've been using GetProperty is not the generic <T> format but rather like this:
I'm not actually sure if these are the same behind the scenes or not. To be honest I've never seen the generic version but can see how it might be very handy in places.
Wow, that solved a lot of my problems! I'm glad I don't have to use this array anymore (thought there was no other option)! I've integrated your line of code in my whole script, and almost everything is working great at this moment!
There's just one little piece of code which does save right, but still renders an error in my browser. I used an Embedded Content DataType, and I want to display all values using a foreach-loop. This used to be working quite well when I was using
@foreach(dynamic item in myNode.subfotos1)
But now, as I replaced 'myNode.subfotos1' with your marvellous piece of code, it just stopped working (it throws me an browser error instead).
Combine field and variable
Right, this should be easy:
How do I combine a field and a variable?
I got something like this:
var myNode = @Model.NodeById(1080);
var testpage = myNode.testpage;
var i = 1;
var mytest = @testpage + @i;
But mytest results only in getting the value of i (which is "1"), instead of "testpage1"
I tried removing the "@" as well (I really have no idea when to use these)
So what should I do? :-D
Hi Peter,
I don't think it's possible to incorporate a variable within a dynamic property name ?
As an alternative, how about using the uQuery .GetProperty<T>() extension method:
HTH,
Hendy
Hi Hendy,
This won't work either :-(
Here's my code:
Isn't there a way to use an array of some kind to solve this?
I mean, I got all these 'var subfotos#' at the top, can't we put these in array?
PS: 'teaserChar' belongs to another piece of code ;-)
As you see, I'm trying to have a loop which creates <a href>'s on the overview page for all 9 subpages. The item I've started to make dynamic is the <h5>.
When I get a solution for that one, I guess I can figure out the other tags (<p>, <img>) by myself. ;-)
At the top you see how I made the link to the "section_#" parts dynamic as well.
Okay, this is what I got so far:
As you can see, I used an array in combination with variable i. But this also doesn't work! When I try '@sub_paginatitel[1]' it all works fine, and when I enter i the file saves correctly. But when I refresh my site after that, I get an error on my site. :-(
I'm so close, where does it go wrong?!?
I tried every usable loop type (for & while), and both won't work.
As soon as I remove the loops and type var i = 1; the var is succesfully included in the array.
So what is my loop doing to this variable that it's not usable for the array anymore?!?
Anyone? :-(
Hello,
I see two things I might be able to suggest. First, regarding your earlier post containing this line:
When you render this page and view the source, you are probably finding that this line right here isn't actually outputting anything? The reason for that would be because the @{ ... } indicates a code block, not actually an instruction to output anything. To illustrate what I mean, notice the difference between these next two lines:
<h5>@{ "someString" }</h5>
The above code block in the H5 tag is just a string that is not output.
<h5>@something</h5>
However the above will.. So you could try chaning your line to this:
Notice all I did was remove the braces (and the semicolon) that were telling Razor to treat as a code block, not as something to ouput. You were really close and this is just one of those Razor things to keep in mind.
OK, now to my second suggestion about your error when you tried putting this in an array. Remember, your umbraco property names all end with 1 through 9... but when you deal with arrays, they start at zero! So looping through your array would need to go from 0 to 8, not 1 to 9. Although I personally would skip the array and all the manual declarations/mappings you are doing, and spend the effor to try and get the loop working using @myNode.GetProperty.
Good luck!
P.S., another way I've been using GetProperty is not the generic <T> format but rather like this:
I'm not actually sure if these are the same behind the scenes or not. To be honest I've never seen the generic version but can see how it might be very handy in places.
Hi Funka,
Wow, that solved a lot of my problems! I'm glad I don't have to use this array anymore (thought there was no other option)!
I've integrated your line of code in my whole script, and almost everything is working great at this moment!
There's just one little piece of code which does save right, but still renders an error in my browser. I used an Embedded Content DataType, and I want to display all values using a foreach-loop. This used to be working quite well when I was using
@foreach(dynamic item in myNode.subfotos1)
But now, as I replaced 'myNode.subfotos1' with your marvellous piece of code, it just stopped working (it throws me an browser error instead).
Here's is my new (enhanced) script:
What am I missing here / doing wrong?
(I also tried to put .Value at the end, but this makes no difference)
And this doesn't work either:
@foreach(dynamic item in myNode.GetProperty("subfotos1"))
This however DOES work without any error:
@foreach(dynamic item in myNode.subfotos1)
Little update:
When I use
@foreach(dynamic item in myNode.GetProperties("subfotos" + u.ToString()))
(I changed from 'GetProperty' to 'GetProperties')
I don't get any errors, but the content inside the foreach-loop still isn't loaded.
Since I'm so close, who can help me out?
Okay, I learned that it's impossible to combine these, and that I needed to use an array instead ('dynamic[]').
Just in case someone wanted to know how this story ended ;-)
This is my solution
is working on a reply...