Copied to clipboard

Flag this post as spam?

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


  • Brian Olsen 143 posts 424 karma points
    Mar 10, 2013 @ 13:53
    Brian Olsen
    0

    get Url from nodeid

    umbraco 4.11.5

    how do i make strlink into a url string 

    strlink = Nodeid

     

    @{

     

    var Teaserslist = @Model.forsideTeasers;

     

      foreach (var Teasers in Teaserslist)

      {

    var strtekst = Teasers.bodyText;

      var strimg = Library.MediaById(Teasers.umbracoFile);

    var strlink = Teasers.link;

     

     @strtekst

     @strlink

     img src="http://mce_host/forum/@strimg" alt="" width="180px" height="102px"

      }

     

    }

     

  • Charles Afford 1163 posts 1709 karma points
    Mar 10, 2013 @ 18:38
    Charles Afford
    0

    Hey :) If you have got the node id, then use @string node = new DynamicNode("24").Url;

    i think this is what you are aksing for?

    Charlie :)

  • Brian Olsen 143 posts 424 karma points
    Mar 11, 2013 @ 08:08
    Brian Olsen
    0

    I can not quite get it to work, do you have an example of a script??

  • Charles Afford 1163 posts 1709 karma points
    Mar 13, 2013 @ 14:22
    Charles Afford
    0

    Umm your best bet may be to look at the dev documentation @ http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets

    I think you might have the same problem as me where you cannot get the currentnode out of a partial?  I assume you are in a partial view here?.

    Does the Node.GetCurrent() return anything?

    If not then are you using a custom model?  Thanks  Charlie :)

  • Charles Afford 1163 posts 1709 karma points
    Mar 13, 2013 @ 14:22
    Charles Afford
    0

    **Sorry for the late reply

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Mar 13, 2013 @ 20:00
    Jeroen Breuer
    1

    For a node url you could use umbraco.library.NiceUrl(id).

    For a media url check the DynamicMedia part here.

    Jeroen

  • Joao 53 posts 172 karma points
    Mar 15, 2013 @ 15:16
    Joao
    0

    Hello! I have a similar problem and I have been struggling with it for quite some time. I think my problems are syntax related.

    <umbraco:Macro runat="server" language="cshtml">
    @{var nodeid}
    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
     @{nodeid = item.highlighLinkToOriginal}
     <a href="@nodeid.Url">Source</a>
    <p>@item.highlighLinkToOriginal</p>
    }
    </umbraco:Macro> 

    I'm trying to provide a link to a page that was chosen by a Content Picker by an editor. That paragraph there was for testing purposes to see if I would get something from @item. highlighLinkToOriginal and I did. This code here wont run. I have read through all the tutorials on Razor I could find but I'm missing soemthing. Can someone help? 

  • Andreas Iseli 150 posts 427 karma points
    Mar 15, 2013 @ 17:25
    Andreas Iseli
    0

    What exactly does not work and what does the highlighLinkToOriginal returns?

  • Joao 53 posts 172 karma points
    Mar 15, 2013 @ 17:27
    Joao
    0

    A node ID.

  • Andreas Iseli 150 posts 427 karma points
    Mar 16, 2013 @ 11:19
    Andreas Iseli
    0

    Just try once

    var node = new Node(item. highlighLinkToOriginal);

    Should work as well:

    var node = @Model.NodeById(item. highlighLinkToOriginal);

    and then inline:

    @node.Url 

     

     

     

     

     

     

     

  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2013 @ 11:36
    Charles Afford
    0

    If you do Nodefactory.Node.GetCurrent();  Are you getting returned or are you getting null?

    You dont need to instansiate a new node, does item. highlighLinkToOriginal have a url property?

    So item.highlighLinkToOriginal.url or item.highlighLinkToOriginal.niceUrl();

    Charlie :) 

     

     

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 12:10
    Joao
    0

    Hello Andreas. I tried writting your code like so:

    <umbraco:Macro runat="server" language="cshtml">

    @{ var node = @Model.NodeById(item. highlighLinkToOriginal);}

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {

    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>
    @*<a href="@item.highlightLinkToOriginal.niceUrl()">Source</a>*@
    }
    </umbraco:Macro>

    It just has the variable declaration but it makes the whole macro not work :/

    Hello Charles. The property highlighLinkToOriginal is a content picker that alows me to choose the node I want. I cleaned the code a little bit:

    <umbraco:Macro runat="server" language="cshtml">
    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlighLinkToOriginal</p>
    <a href="@item.highlightLinkToOriginal.niceUrl()">Source</a>
    }
     </umbraco:Macro>

    I tried both .Url and .niceUrl(). Both cause the whole macro to not be loaded. When I remove that line of code everything works fine. In the paragraph I get a number - 1218 - which I believe it's the nodeID for the content node I chose with the Content Picker. 

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 12:12
    Joao
    0

    On a side note, I changed the property name from highlighLinkToOriginal to highlightLinkToOriginal and I double checked every where to see if it's correct.

     

    I also realized that puting this code:

    @{
    var node = @Model.NodeById(item.highlightLinkToOriginal);
    }

     

    outside of the foreach would not make sense since the var item is created in the foreach. I changed it like so:

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    @{
    var node = @Model.NodeById(item.highlightLinkToOriginal);
    }
    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>
    @*<a href="@item.highlightLinkToOriginal.niceUrl()">Source</a>*@
    }

    and the macro still fails. 

  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2013 @ 12:43
    Charles Afford
    0

    You need to pass node into your a href: So <a href="@node.niceUrl();">

    Additonaly var Node should be

    umbraco.umbracoNodeFactory.Node node = .... 

    Charlie :)

  • Andreas Iseli 150 posts 427 karma points
    Mar 16, 2013 @ 13:43
    Andreas Iseli
    0

    Charlie is absolute right. Hope this helps you to figure out the correct way ;)

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 14:28
    Joao
    0

    Hello again Charles. I think I did like you said but I still get a macro error meaning I have wrong syntax. Here's the code

    <umbraco:Macro runat="server" language="cshtml">

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
      @umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal);
    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>
    <a href="@node.niceUrl();">Source</a>
    }
    </umbraco:Macro> 

  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2013 @ 14:39
    Charles Afford
    0

    Hello

    try <a href="node.niceUrl></a>

    If you take the <a href=""> bit out do you get any xslt errors?

    Can you tell me what you are getting from 

    node 

     and

    @Model.NodeById(item.highlightLinkToOriginal);

    :)

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 14:53
    Joao
    0

    Removing <a href="@node.niceUrl();">Source</a> completely changes nothing, i still get the error: Error loading MacroEngine script (file: )

    I don't get errors if I remove: @umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal); as well. I can't tell you what node yields.

    But with these two lines removed, I tried inserting this:

    <p>@Model.NodeById(item.highlightLinkToOriginal);</p>

    and in the page I get:

    umbraco.MacroEngines.DynamicNode; 

     

     

     

  • Andreas Iseli 150 posts 427 karma points
    Mar 16, 2013 @ 14:56
    Andreas Iseli
    0

    Try please @node.Url or @node.NiceUrl. That should be properties not a method in this case.

     

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 15:00
    Joao
    0

    As soon as I insert this inside the macro it stops working:

    @umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal); 

  • Brian Olsen 143 posts 424 karma points
    Mar 16, 2013 @ 15:13
    Brian Olsen
    100

    i got it working now by using this

    var strlinkid = Teasers.link;

    var strlink = @Model.NodeById(@strlinkid);

     

    @strlink.Url

  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2013 @ 15:20
    Charles Afford
    0

    string nodeUrl = @Model.NodeById(item.highlightLinkToOriginal).niceurl();

    nodeUrl should now be the nice url?

  • Andreas Iseli 150 posts 427 karma points
    Mar 16, 2013 @ 15:44
    Andreas Iseli
    0

    Please change

    @umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal); 

    to

    var node = Model.NodeById(item.highlightLinkToOriginal); 

     

    You have to many @ in your code. Please double check your razor syntax carefully.

  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 15:45
    Joao
    0

    I should add that line like so right?

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {

    <h2>@item.highlightTitle</h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>
    <p>@Model.NodeById(item.highlightLinkToOriginal);</p>
    @string nodeUrl = @Model.NodeById(item.highlightLinkToOriginal).niceUrl();
      <p>@nodeUrl</p>   

    When I add it nothing works, the macro fails to load. Error loading MacroEngine script (file: ) If I remove the @ before string the macro does not fail but it also does not yield anything. Am I missing some using statment? Currently I only have @using umbraco.MacroEngines;

  • Andreas Iseli 150 posts 427 karma points
    Mar 16, 2013 @ 15:48
    Andreas Iseli
    0
    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
        var node = @Model.NodeById(item.highlightLinkToOriginal);
        <h2>@item.highlightTitle</h2>
        <div class="HighText"><p>@item.highlightText</p></div>
        <p>@item.highlightLinkToOriginal</p>
        <p>@node.Url</p>   
    } 
  • Joao 53 posts 172 karma points
    Mar 16, 2013 @ 15:51
    Joao
    0

    I will check it throurougly later tonight, I have to run now. Thank you so much for you patience and help. I will let you know how it went.

  • Joao 53 posts 172 karma points
    Mar 17, 2013 @ 12:47
    Joao
    0

    Hello again! I got it to work. Changing

    @umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal); 

    to

    var node = Model.NodeById(item.highlightLinkToOriginal);

    Did the trick. Here's the completed code:

     

    <umbraco:Macro runat="server" language="cshtml">

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    var node = Model.NodeById(item.highlightLinkToOriginal);
    <h2><a href="@node.Url">@item.highlightTitle</a></h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    }
    </umbraco:Macro>  

     

    Cheers! 

  • Charles Afford 1163 posts 1709 karma points
    Mar 17, 2013 @ 13:25
    Charles Afford
    0

    joan :), you could just do

    <a href="Model.NodeById(item.highlightLinkToOriginal).url"></a>

    I think that should work.  Saves the overhead of having to create x instances of var each time just to get a url. Charlie :)

  • Joao 53 posts 172 karma points
    Mar 17, 2013 @ 22:03
    Joao
    0

    Hello. Tried to put this: <a href="Model.NodeById(item.highlightLinkToOriginal).url"></a> but it didn't work. The stranges thing now is that when I reverted back to what it was it stopped working. I copy pasted what I have so I have no idea why it's not working now :(

  • Andreas Iseli 150 posts 427 karma points
    Mar 18, 2013 @ 09:08
    Andreas Iseli
    0

    Please everyone ensure case sensitive writing and the razor syntax:

    <a href="@Model.NodeById(item.highlightLinkToOriginal).Url"></a>
  • Joao 53 posts 172 karma points
    Mar 18, 2013 @ 10:38
    Joao
    0

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    var node = Model.NodeById(item.highlightLinkToOriginal);
    <h2><a href="@node.Url">@item.highlightTitle</a></h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    }

    The syntax is correct here but the macro still fails to load. 

  • Andreas Iseli 150 posts 427 karma points
    Mar 18, 2013 @ 10:43
    Andreas Iseli
    0

    on which line does it fail? Please remove the whole code and add back it step by step.

  • Joao 53 posts 172 karma points
    Mar 18, 2013 @ 10:54
    Joao
    0

    Right on the first line, the variable declaration fails:

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    var node = Model.NodeById(item.highlightLinkToOriginal);

    }

     

  • Joao 53 posts 172 karma points
    Mar 18, 2013 @ 11:09
    Joao
    0

    Did some more testing. First I made it write in a <p> what @item.highlightLinkToOriginal returns simply by:

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
    <h2><a href="">@item.highlightTitle</a></h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>

    It wrote the number 1218. I then placed that number inside the var declaration and it all worked as expected:

    @foreach (var item in @Model.Highlight.Where("Visible"))
    {
      var node = Model.NodeById(1218);
    <h2><a href="@node.Url">@item.highlightTitle</a></h2>
    <div class="HighText"><p>@item.highlightText</p></div>
    <p>@item.highlightLinkToOriginal</p>

    The moment I do  var node = Model.NodeById(item.highlightLinkToOriginal); it fails to load.

  • Joao 53 posts 172 karma points
    Mar 18, 2013 @ 11:26
    Joao
    0

    And I got it to work. I now know why. The foreach cycle runs 6 times and if one of those item.highlightLinkToOriginal is empty it will fail to load everything. Time to make that property mandatory!

  • Andreas Iseli 150 posts 427 karma points
    Mar 18, 2013 @ 11:28
    Andreas Iseli
    0

    I'm glad it works now :)

  • Charles Afford 1163 posts 1709 karma points
    Mar 18, 2013 @ 22:34
    Charles Afford
    0

    Jaon i see when you need the node by id now, unless there is a particular reason why you are getting the node by id you should try and get it another way.  If that node changes at all (Deleted, added again ect) the code will have to change.  Also if the code is failing to run on the 6th time you should do some check or something to get the data out 5 times and not rely on making the fields mandatory. Only my opinion of course but feels like you are boxing your self in here :).  Charlie.

Please Sign in or register to post replies

Write your reply to:

Draft