Copied to clipboard

Flag this post as spam?

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


  • Roger 195 posts 474 karma points
    Sep 16, 2015 @ 12:10
    Roger
    0

    New to v7 and Razor - Simple NiceUrl link

    Hi all, Im just moving over to v7 and MVC from v6 and its all a bit of a learning curve again. Just trying to add a simple link into a template.

    In v6 I would use:

    <a href="<umbraco:Item runat="server" field="LINK" xslt="umbraco.library:NiceUrl({0})" />LINK</a>
    

    Now it seems so difficult to either find documentation on simple things like this using Razor or its just more complicated to implement.

    I've tried:

    @Umbraco.NiceUrl('LINK', recursive: true)
    

    but i get a runtime error

    How do i add a simple NiceUrl link that works similar to v6?

    Thanks

  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 12:51
    Steve Morgan
    0

    Is the field part of the current document?

    Just use

    <a href="@Model.Content.Url" />LINK</a>
    

    or

    <a href="@CurrentPage.Url" />LINK</a>
    

    If you need the full URL

    <a href="@(Model.Content.UrlAbsolute())" />LINK</a>
    
  • Roger 195 posts 474 karma points
    Sep 16, 2015 @ 13:03
    Roger
    0

    and I can write that straight into the template?

    I was thinking that it might have been simplified in V7 from V6 not more complex :-/

  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 13:04
    Steve Morgan
    0

    Sorry - I posted my reply in the wrong place - then deleted it! Roger is referring to my response below.

  • Roger 195 posts 474 karma points
    Sep 16, 2015 @ 12:55
    Roger
    0

    Hi, thanks for the reply, No it isnt part of the current page.

    Its a link in a template that is selected using a content picker and needs to be recursive with it being in the master template

    In V6 i would use:

    <a href="<umbraco:Item runat="server" field="Link1" xslt="umbraco.library:NiceUrl({0})" />" >CLICK HERE</a>
    
  • Roger 195 posts 474 karma points
    Sep 16, 2015 @ 13:00
    Roger
    0

    Just to give you a visual, its the advert link here...

    enter image description here

  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 13:02
    Steve Morgan
    0

    Ahh..

    @{
                    int nodeId = Model.Content.GetPropertyValue<int>("advertLink", true);
                    var linkNode = Umbraco.TypedContent(nodeId);
                    <a href="@(linkNode.Url)">LINK</a>
    }
    
  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 13:07
    Steve Morgan
    100

    OK - if you want a one liner... though I don't think it's as readable.

    <a href="@(Umbraco.TypedContent(Model.Content.GetPropertyValue<int>("AdvertLink", true)).Url)">LINK</a>
    

    Yes you can put this straight into a template as they are Razor. There is a question of coding style how much logic you put in your templates. I think this is probably fine

  • Alex Skrypnyk 6060 posts 23282 karma points MVP 6x admin c-trib
    Sep 16, 2015 @ 13:07
    Alex Skrypnyk
    1

    Hi Roger,

    On each page you have Umbraco context. Content picker stores id to the destination page. So you have to get this id and get url to this page from Umbraco context.

    You can do that like that :

    var nodeId = Umbraco.AssignedContentItem.GetPropertyValue<int>("Link1");
    var node = Umbraco.TypedContent(nodeId);
    var url = node.Url;
    

    Thanks, Alex

    https://ua.linkedin.com/pub/alexander-skripnik/66/60b/352

  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 13:15
    Steve Morgan
    0

    Hi Alex,

    Interesting use of AssignedContentItem - can you explain why you use that over CurrentPage / Model.Content ?

    I think you'd also need to add the recursive boolean to your GetPropertyValue call for that to work as Roger needs?

    var nodeId = Umbraco.AssignedContentItem.GetPropertyValue<int>("Link1", true);
    
  • Roger 195 posts 474 karma points
    Sep 16, 2015 @ 13:27
    Roger
    0

    Thanks so much guys,

    Steve, i used your inline method with the recursive boolean and it works great!

    Its all new to me so a big learning curve on the way I think!

    Thanks again!

  • Steve Morgan 1301 posts 4351 karma points c-trib
    Sep 16, 2015 @ 13:55
    Steve Morgan
    1

    I'd recommend using Visual Studio for Razor.. the intelisense is a life saver!

    These are also helpful (says v6 but good for v7 too).

    https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

  • Roger 195 posts 474 karma points
    Sep 17, 2015 @ 08:50
    Roger
    0

    Yeah i do use VS as a rule. Im just new to Razor, only just moving to umbraco V7+ so i think its a learning curve for me!

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft