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?
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.
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>*@ }
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:
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;
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 :(
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!
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.
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"
}
}
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 :)
I can not quite get it to work, do you have an example of a script??
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 :)
**Sorry for the late reply
For a node url you could use umbraco.library.NiceUrl(id).
For a media url check the DynamicMedia part here.
Jeroen
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?
What exactly does not work and what does the highlighLinkToOriginal returns?
A node ID.
Just try once
var node = new Node(item. highlighLinkToOriginal);
Should work as well:
var node = @Model.NodeById(item. highlighLinkToOriginal);
and then inline:
@node.Url
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 :)
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.
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.
You need to pass node into your a href: So <a href="@node.niceUrl();">
Additonaly var Node should be
umbraco.umbracoNodeFactory.Node node = ....
Charlie :)
Charlie is absolute right. Hope this helps you to figure out the correct way ;)
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>
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);
:)
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;
Try please @node.Url or @node.NiceUrl. That should be properties not a method in this case.
As soon as I insert this inside the macro it stops working:
@umbraco.umbracoNodeFactory.Node node = @Model.NodeById(item.highlightLinkToOriginal);
i got it working now by using this
var strlinkid = Teasers.link;
var strlink = @Model.NodeById(@strlinkid);
@strlink.Url
string nodeUrl = @Model.NodeById(item.highlightLinkToOriginal).niceurl();
nodeUrl should now be the nice url?
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.
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;
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.
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!
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 :)
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 :(
Please everyone ensure case sensitive writing and the razor syntax:
<a href="@Model.NodeById(item.highlightLinkToOriginal).Url"></a>
@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.
on which line does it fail? Please remove the whole code and add back it step by step.
Right on the first line, the variable declaration fails:
@foreach (var item in @Model.Highlight.Where("Visible"))
{
var node = Model.NodeById(item.highlightLinkToOriginal);
}
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.
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!
I'm glad it works now :)
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.
is working on a reply...