Copied to clipboard

Flag this post as spam?

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


  • Karel-Jan Van Haute 5 posts 85 karma points
    Mar 20, 2017 @ 09:17
    Karel-Jan Van Haute
    0

    Umbraco.TagQuery.GetContentByTag

    To start of. I'm new to umbraco.

    I have the following. When it takes no tag in the URL. So it takes CurrentPage.Children the code works. But when I am loading the TagQuery I get an error saying that overviewQuote is not defined.

    @{
        Layout = "Master.cshtml";
        string tag = Request.QueryString["tag"];
    
        var stories = CurrentPage.Children;
        if(!tag.IsNullOrWhiteSpace()){
            stories = Umbraco.TagQuery.GetContentByTag(tag, "stories");
        }
    }
    
    <a href="@story.Url">
        <h2>@story.overviewQuote</h2>       
        <img src="@Umbraco.Media(story.overviewImage).Url" alt="@Umbraco.Media(story.overviewImage).Name">
    </a>
    

    When I do this it works in both scenarios, but the image does not get loaded. The code says "undefined".

    @{
        Layout = "Master.cshtml";
        string tag = Request.QueryString["tag"];
    
        var stories = CurrentPage.Children;
        if(!tag.IsNullOrWhiteSpace()){
            stories = Umbraco.TagQuery.GetContentByTag(tag, "stories");
        }
    }
    
    <a href="@story.Url">
        <h2>@Umbraco.Field(story, "overviewQuote")</h2>     
        <img src="@Umbraco.Media(@Umbraco.Field(story, "overviewImage")).Url" alt="@Umbraco.Media(@Umbraco.Field(story, "overviewImage")).Name">
    </a>
    

    Can someone please tell me what the best practice is to do this?

    thanks

  • Richard Eyres 98 posts 580 karma points
    Mar 20, 2017 @ 09:33
    Richard Eyres
    100

    Personally, i would avoid using the Dynamic model and instead use Model.Content instead of CurrentPage.

    Then get the property data via the GetPropertyValue method

    story.GetPropertyValue<string>("overviewQuote")
    
  • Karel-Jan Van Haute 5 posts 85 karma points
    Mar 20, 2017 @ 09:39
    Karel-Jan Van Haute
    0

    That's just perfect. Now it all works. Thank you very much !

    Just for future reference. What's the difference between the two. And when do you use which one?

  • Richard Eyres 98 posts 580 karma points
    Mar 20, 2017 @ 09:44
    Richard Eyres
    0

    No worries, glad it worked out for you.

Please Sign in or register to post replies

Write your reply to:

Draft