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
    Sep 04, 2012 @ 17:04
    Peter Schermers
    0

    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:

    string[] subfotos = {
    @myNode.subfotos1,
    @myNode.subfotos2
    };

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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 04, 2012 @ 18:19
    Tom Fulton
    0

    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

  • Peter Schermers 112 posts 134 karma points
    Sep 05, 2012 @ 09:17
    Peter Schermers
    0

    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:

     

    @{
    var myNode     = @Model.NodeById(1080);
    var quotes     = @myNode.quotes;
    var teaserChar = 90;
    var sections   = "";
    var sectionid  = "";

    <div id="portfolio">
    <div class="container">
    <ul id="wrapper">
    <li id="section_home" class="section">
    <div class="row">
    <div class="titel">
    <h1>@myNode.titel&nbsp;&nbsp;|</h1><h2>@myNode.slogan</h2>
    </div>
    </div>
    <div class="row header">
    <div class="span6">
    @myNode.inleiding
    </div>
    <div class="span6 quotes">
    <div class="row">
    <div class="span2">
    <img src="../images/quote.png" />
    </div>
    <div class="span4">
    <div class="quote">

    @foreach(dynamic item in quotes)
    {  
    <div>
    <h5>@item.quoteTitel.InnerText</h5>
    <p>@item.quoteTekst.InnerText</p>
    <span>@item.quoteBedrijf.InnerText&nbsp;&nbsp;|&nbsp;&nbsp;@item.quoteContact.InnerText</span>
    </div>
    }

    </div>
    </div>
    </div>
    </div>
    </div>
    <div class="row content">

    @for (var i = 1; i <= 9; i++)
    {
    sections = "#section_" + i;
    if(myNode.GetProperty("subhide" + i.ToString()).Value != "1")
    {
    <a href="@sections">
    <div class="span4">
    <img src="@Library.MediaById(@myNode.GetProperty("subfoto" + i.ToString()).Value).Url" />
    <h5>@myNode.GetProperty("sub_paginatitel" + i.ToString()).Value</h5>
    <p>@Library.StripHtml(@Library.Truncate(@myNode.GetProperty("sub_inleiding" + i.ToString()).Value, teaserChar))</p>
    </div>
    </a>
    }
    }

    </div>
    </li>

    @for (var u = 1; u <= 9; u++)
    {
    sectionid = "section_" + u;
    if(myNode.GetProperty("subhide" + u.ToString()).Value != "1")
    {
    <li id="@sectionid" class="section">
    <div class="row">
    <div class="titel">
    <h1>@myNode.GetProperty("sub_paginatitel" + u.ToString()).Value&nbsp;&nbsp;|</h1>
    <h2>@myNode.GetProperty("sub_slogan" + u.ToString()).Value</h2>
    </div>
    </div>
    <div class="row header">
    <div class="span6">
    @myNode.GetProperty("sub_inleiding" + u.ToString())
    <a href="#section_home" class="port_terug">< overzicht</a>
    </div>
    <div class="span6 quotes">
    <div class="row">
    <div class="span2">
    <img src="../images/quote.png" />
    </div>
    <div class="span4">
    <div class="quote">

    @foreach(dynamic item in quotes)
    {
    <div>
    <h5>@item.quoteTitel.InnerText</h5>
    <p>@item.quoteTekst.InnerText</p>
    <span>@item.quoteBedrijf.InnerText&nbsp;&nbsp;|&nbsp;&nbsp;@item.quoteContact.InnerText</span>
    </div>
    }

    </div>
    </div>
    </div>
    </div>
    </div>
    <div class="row content">

    @foreach(dynamic item in @myNode.GetPropertyValue("subfotos1"))
    {
    <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>
    }

    </div>
    </li>
    }
    }

    </ul>
    </div>
    </div>
    }

     

  • Peter Schermers 112 posts 134 karma points
    Sep 05, 2012 @ 09:21
    Peter Schermers
    0

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

  • Peter Schermers 112 posts 134 karma points
    Sep 05, 2012 @ 09:24
    Peter Schermers
    0

    By the way, isn't there some sort of debugger so I can see where it goes wrong with my Razor code (just asking)?

  • Peter Schermers 112 posts 134 karma points
    Sep 05, 2012 @ 09:26
    Peter Schermers
    0

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

  • Peter Schermers 112 posts 134 karma points
    Sep 05, 2012 @ 09:32
    Peter Schermers
    0

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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 05, 2012 @ 17:27
    Tom Fulton
    0

    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:

    dynamic[] subfotos = { myNode.subfotos1, myNode.subfotos2 };

    -Tom

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

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

Please Sign in or register to post replies

Write your reply to:

Draft