Copied to clipboard

Flag this post as spam?

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


  • Joao 53 posts 172 karma points
    Sep 18, 2013 @ 15:09
    Joao
    0

    Problem with GetProperty

    Hello!

    First I should start by explaining the context of the problem. This is my content structure:

    When I am in the Master Swedish page, I am running a razor script that is supposed to go the the Master Node, search all blog posts and show the ones that have the swedishChoice property set to true, like so:

    @using umbraco.MacroEngines;
    @{
    DynamicNode contentRoot = new DynamicNode(-1);
    }
    
    @foreach (DynamicNode n in contentRoot.Descendants("BlogPost")){
    
    if(@n.GetProperty("swedishChoice") == 1){
        <div class="mp_blog">
        <div class="mp_blog_Preview">@n.GetProperty("blogText")</div>
        <div class="mp_blog_Date">@n.CreateDate.ToString("dd.MM.yyyy")</div>
        <div class="mp_blog_Link"><a href="@n.Url">lue lisää</a></div>
        </div><!--mp_blog-->}
    }

    What I get is an error saying that the script could not be loaded and it happens because I put the @n.GetProperty("swedishChoice") in the if statement. Shoulndn't this work?

    I suspect this has something to do with the way I am accessing the Master Node. Is there a better way? I tried accessing the BlogPost nodes by creating this variable:

    var blogs = @Model.Up().Descendants("BlogPost");
    

    but I get the same type of error.

    Thank you for your time.

  • Moran 285 posts 934 karma points
    Sep 18, 2013 @ 16:09
    Moran
    0

    Have you tried ?

    Model.NodeById(pageId).Descendants("BlogPost");
    

    And I think you should use:

    GetPropertyValue instead of GetProperty

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 18, 2013 @ 16:24
    Ali Sheikh Taheri
    101

    Hi Joao

    you should either use

    <div class="mp_blog_Preview">@n.GetProperty("blogText").Value</div>
    

    or

    <div class="mp_blog_Preview">@n.GetPropertyValue("blogText")</div>
    

    so your code must be like below:

    @using umbraco.MacroEngines;
    @{
    DynamicNode contentRoot = new DynamicNode(-1);
    }
    
    @foreach (DynamicNode n in contentRoot.Descendants("BlogPost").Items){
    
    if(@n.GetPropertyValue("swedishChoice") == "1"){
        <div class="mp_blog">
        <div class="mp_blog_Preview">@n.GetPropertyValue("blogText")</div>
        <div class="mp_blog_Date">@n.CreateDate.ToString("dd.MM.yyyy")</div>
        <div class="mp_blog_Link"><a href="@n.Url">lue lisää</a></div>
        </div><!--mp_blog-->}
    }
    
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 18, 2013 @ 16:39
    Ali Sheikh Taheri
    0

    I've also added .Items to Descendants(BlogPost") to get the return type List<DynamicNode>

  • Joao 53 posts 172 karma points
    Sep 19, 2013 @ 16:38
    Joao
    0

    Using GetPropertyValue worked well! Also having the number 1 in between " " also did the trick. Thanks.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies