Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Apr 09, 2014 @ 18:33
    Jason Espin
    0

    Problems with related links in Umbraco 7.1.1

    Hi all,

    I've just tried to include a related links field within one of my pages. The menu and interface of inserting the links and adding a caption all seem to work correctly however, when it comes to the output on the actual site I'm seeing what looks like JSON (See below). Is this the way in which this datatype is supposed to be presented in Umbraco 7? If so, how is this then converted into useable anchor links within a site?

    [ { "caption": "Om Jesper Hannibal", "link": "/", "newWindow": false, "internal": "1058", "edit": true, "isInternal": true, "internalName": "Home", "type": "internal", "title": "Om Jesper Hannibal" } ] 
    

    Thanks in advance.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 09, 2014 @ 18:43
    Jeavon Leopold
    0

    Hi Jason,

    Please find sample Razor code for both Typed and Dynamic models here

    Jeavon

  • Jason Espin 368 posts 1335 karma points
    Apr 10, 2014 @ 10:55
    Jason Espin
    0

    Hi Jeavon,

    Thanks for the reply. As I mentioned earlier I am new to MVC so could you briefly summise in which cases each would be preferred as I am unsure at this point in time as to the difference between typed and dynamic.

    My code block is to be stored in the MasterLayout for my site which contains the header and footer. The related links are to be displayed in the footer of each page and are defined as properties of the home content page which is the root content page of my site.

    I managed to find the following code elsewhere but are the methods you have enclosed above more scalable (e.g. will this fail if there are no related links in the home content page or will it just not render anything.

     @{
         var relatedLinks = CurrentPage.AncestorsOrSelf(1).First().relatedLinks;
         foreach (var linkItem in relatedLinks){
                 string linkUrl;
                 string linkTarget;
    
                 if ((bool)linkItem.isInternal){
                   linkUrl = (string)linkItem.link;
                   linkTarget = string.Empty;
                 }else{
                   linkUrl = linkItem.link;
                   linkTarget = " target=\"_blank\"";
                 }
    
                 <li><a class="footerNavigation" href="@linkUrl" @Html.Raw(linkTarget)>@linkItem.caption</a></li>
         }
     }
    

    Also, at the moment I am using this code inline within my master template. Is it acceptible to do this or should this code be stored in a partial view and rendered to the page using @{ Html.RenderPartial("RelatedLinks");} for example?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 10, 2014 @ 11:04
    Jeavon Leopold
    100

    Hi Jason,

    The code snippets in the documentation are well tested and have already been tweaked and improved a few times, I would recommend using them.

    I do have a package which I think makes working with some of the core property editors including related links much easier in terms of Razor, epecially for starters. You can find it here If you install you will need to adjust and Razor you have for the editors it converts. This is the Razor snippet you can use if you install it

    With regard to Typed vs Dynamic models, it is really a personal preference. Fundamentally Typed gives you code Intellisense in Visual Studio or WebMatrix but Dynamic is more concise so if you are only using the web based template editor only it is generally preferred.

    Any further questions let me know.

    Jeavon

  • Jason Espin 368 posts 1335 karma points
    Apr 10, 2014 @ 11:31
    Jason Espin
    0

    Hi Jeavon,

    This solution does not seem to persist across pages. For some reason, the related links are only display on the homepage. This sort of makes sense as this is where the related links are defined but how can I ensure that the related links are accessible to all children of the homepage (as the call to the related links partial view is made in my master template)?

    Cheers Jason

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 10, 2014 @ 17:19
    Jeavon Leopold
    0

    Hi Jason,

    Which code did you use in the end? Could you post it and then I can help you make it recursive so that it will look up the tree form where is being executed to find the first node that has the "relatedLinks" property and render that out.

    Jeavon

  • Jason Espin 368 posts 1335 karma points
    Apr 10, 2014 @ 17:41
    Jason Espin
    0

    Hi Jeavon,

    I used the following code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Newtonsoft.Json.Linq
    @{      
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    <li><a class="footerNavigation" href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                }
            </ul>
        }
    }  
    

    This is stored within a partial view and is called in my master template using:

     @{Html.RenderPartial("relatedLinks");}
    

    Thanks for all of your help so far.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 10, 2014 @ 17:56
    Jeavon Leopold
    0

    No problem, to make this recursive so that it will look up the tree (to the homepage) from where it is rendering until it finds a node with the "relatedLinks" property, all you need to do is add three "true" parameters, so it would be like this:

     @{      
        if (Model.Content.HasValue("relatedLinks", true) && Model.Content.GetPropertyValue<string>("relatedLinks", true).Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks", true))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                }
            </ul>
        }
    } 
    
  • Jason Espin 368 posts 1335 karma points
    Apr 10, 2014 @ 18:17
    Jason Espin
    0

    Great. Thanks. This worked a treat! One more question. If I wanted to count the number of links I am outputting and append the count to the end of the link, would I be able to do the following:

     var linkCount = 0;
    
     @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks", true)){
         var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
         var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
         <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption")) Link@imageCount</a></li>
         @imageCount ++;
     }
    

    Or are variables such as this handled in a different way in Umbraco? (Apologies if this is a stupid question, I'm a Javascript developer by trade so not too sure on how this is handled in C# / Umbraco.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 10, 2014 @ 18:48
    Jeavon Leopold
    0

    Awesome!

    Pretty close:

            @{
                var imageCount = 0;
                foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks", true))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    imageCount ++;
    
                    <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption")) @("Link" + imageCount)</a></li>
                }
            }
    
  • Jason Espin 368 posts 1335 karma points
    Apr 11, 2014 @ 10:01
    Jason Espin
    1

    Great. Thanks. I was pretty close! Once I start getting used to the Razor syntax I should be okay. Thanks for all of your help!

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 11, 2014 @ 12:12
    Jeavon Leopold
    0

    Awesome! This bit @("Link" + imageCount) is a little annoying, if there was a space it could just be Link @imageCount

  • Jason Espin 368 posts 1335 karma points
    Apr 11, 2014 @ 12:34
    Jason Espin
    0

    Yeah but that's concatenation for you!

  • sreejith 10 posts 109 karma points
    Jun 24, 2014 @ 19:13
    sreejith
    0

    Hi Guys,

    I dont know whether is this the right place to ask this question. I need to assign the related link property values to my Back Office using Surface Controller. Any body knows the proper way to do it?.

    I have to do it as the after effect of some user submitted Data. When user submits data, a content page is automatically generated using the surface Controller.  And I have done that. Now I need to set the media files into a related link propery value of the generated file.

    Any help would be appreciated.

Please Sign in or register to post replies

Write your reply to:

Draft