Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Schermers 112 posts 134 karma points
    Aug 30, 2012 @ 16:13
    Peter Schermers
    0

    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

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 30, 2012 @ 17:02
    Hendy Racher
    0

    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:

    @using umbraco;
    @using umbraco.NodeFactory;
    <ul>
    @{    
      Node node = new Node(1080);
    
      for(int i = 0; i < 9; i++)
      {
        <li>@(node.GetProperty<string>("testpage" + i.ToString()))</li>
      }
    }
    </ul>
    

    HTH,

    Hendy

  • Peter Schermers 112 posts 134 karma points
    Aug 31, 2012 @ 08:52
    Peter Schermers
    0

    Hi Hendy,

     

    This won't work either :-(

    Here's my code:

     

     

    @using umbraco;
    @using umbraco.NodeFactory;
    @{
      var myNode     = @Model.NodeById(1080);
      var subfotos1  = myNode.subfotos1;
      var subfotos2  = myNode.subfotos2;
      var subfotos3  = myNode.subfotos3;
      var subfotos4  = myNode.subfotos4;
      var subfotos5  = myNode.subfotos5;  
      var subfotos6  = myNode.subfotos6;  
      var subfotos7  = myNode.subfotos7;  
      var subfotos8  = myNode.subfotos8;
      var subfotos9  = myNode.subfotos9;
      var sections   = "";
      var teaserChar = 90;
      @for (var i = 1; i <= 9; i++)
            {
                sections = "section_" + i;
                 <a href="#@sections">
                  <div class="span4">
                    <img src="@Library.MediaById(@myNode.subfoto1).Url" />
                    <h5>@{myNode.GetProperty<string>("sub_paginatitel" + i.ToString());}</h5>
                    <p>@Library.StripHtml(@Library.Truncate(@myNode.sub_inleiding1, teaserChar))</p>
                  </div>
                </a>
            }
    }

    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?

  • Peter Schermers 112 posts 134 karma points
    Aug 31, 2012 @ 08:53
    Peter Schermers
    0

    PS: 'teaserChar' belongs to another piece of code ;-)

  • Peter Schermers 112 posts 134 karma points
    Aug 31, 2012 @ 09:02
    Peter Schermers
    0

    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.

  • Peter Schermers 112 posts 134 karma points
    Aug 31, 2012 @ 09:45
    Peter Schermers
    0

    Okay, this is what I got so far:

    @{
      var myNode     = @Model.NodeById(1080);
      var subfotos1  = myNode.subfotos1;
      var subfotos2  = myNode.subfotos2;
      var subfotos3  = myNode.subfotos3;
      var subfotos4  = myNode.subfotos4;
      var subfotos5  = myNode.subfotos5;  
      var subfotos6  = myNode.subfotos6;  
      var subfotos7  = myNode.subfotos7;  
      var subfotos8  = myNode.subfotos8;
      var subfotos9  = myNode.subfotos9;
      var sections   = "";
      var teaserChar = 90;
      string[] sub_paginatitel = {
        @myNode.sub_paginatitel1,
        @myNode.sub_paginatitel2,
        @myNode.sub_paginatitel3,
        @myNode.sub_paginatitel4,
        @myNode.sub_paginatitel5,
        @myNode.sub_paginatitel6,
        @myNode.sub_paginatitel7,
        @myNode.sub_paginatitel8,
        @myNode.sub_paginatitel9,
      };
    @for (var i = 1; i <= 9; i++)
              {
                sections = "section_" + i;

                     

                 <a href="#@sections">
                  <div class="span4">
                    <img src="@Library.MediaById(@myNode.subfoto1).Url" />
                    <h5>@sub_paginatitel[i]</h5>
                    <p>@Library.StripHtml(@Library.Truncate(@myNode.sub_inleiding1, teaserChar))</p>
                  </div>
                </a>
              }

    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?!?

  • Peter Schermers 112 posts 134 karma points
    Aug 31, 2012 @ 14:06
    Peter Schermers
    0

    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? :-( 

  • Funka! 398 posts 661 karma points
    Aug 31, 2012 @ 19:09
    Funka!
    1

    Hello,

    I see two things I might be able to suggest. First, regarding your earlier post containing this line:

    <h5>@{myNode.GetProperty<string>("sub_paginatitel" + i.ToString());}</h5>

    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:

    <h5>@myNode.GetProperty<string>("sub_paginatitel" + i.ToString())</h5>

    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:

    <h5>@myNode.GetProperty("sub_paginatitel" + i.ToString()).Value</h5>

    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.

  • Peter Schermers 112 posts 134 karma points
    Sep 03, 2012 @ 09:19
    Peter Schermers
    0

    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:

     

    @foreach(dynamic item in myNode.GetProperty("subfotos" + u.ToString()))
              {
                 <a href="@Library.MediaById(@item.subfotolight.InnerText).Url" class="group1" title="@item.subonderschrift.InnerText">
                  <div class="span4">
                    <img src="@Library.MediaById(@item.subfotolight.InnerText).Url" />
                    <h5>@item.subtitel.InnerText</h5>
                  </div>
                </a>
              }     

    What am I missing here / doing wrong?

    (I also tried to put .Value at the end, but this makes no difference)

  • Peter Schermers 112 posts 134 karma points
    Sep 03, 2012 @ 10:20
    Peter Schermers
    0

    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)

  • Peter Schermers 112 posts 134 karma points
    Sep 03, 2012 @ 10:32
    Peter Schermers
    0

    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? 

  • Peter Schermers 112 posts 134 karma points
    Sep 06, 2012 @ 16:07
    Peter Schermers
    0

    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 ;-)

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 08, 2016 @ 22:23
    Biagio Paruolo
    0

    This is my solution

    @{
                             var i=1;
                            }   
    
                             @foreach (var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("cartoline"))
                                {       
                                    var image = Umbraco.Media(@fieldset.GetValue("immagine"));
                                    <div class="@(i==1 ? "tab-pane fade in active" : "tab-pane fade in")" id='tab@(i)'>
    
                                    </div>
                                    i++;
    
                                }
    
Please Sign in or register to post replies

Write your reply to:

Draft