I just got into razor so i'm still pretty green in this area.
I got this properties for a link setup in umbraco
...and up to the client to pick wich kind of link they would like to use on the item they are creating.
And my code should do something like this:
@*If no kind of link is choosen the # sign will be my href="#" a kind of fallback propertie *@
Var boxLink = "#";
if internalLink HasValue change variable to
boxlink = Library.NodeById(internalLink).Url;
if externalLink HasValue change variable to
boxlink = http://myLink.dk;
....
and at last i would like to put my boxLink var value inside my
<a href="@boxlink">Read more</a>
An right now my code looks like this:
@{
var link = "#";
if(Boxes.HasValue("internalLink"))
{
link = Library.NodeById(Boxes.internalLink);
}
<a href="#">@link.Url</a>
}
Yes, you should use Partial Views (for reuse) or Razor directly in your template. Partial View Macros should be used when you need parameters such as inserting a macro into the RTE (fairly rare use cases).
Try the following in either a Partial View or directly in your cshtml template:
Razor change variable value
Hi guys
I just got into razor so i'm still pretty green in this area.
I got this properties for a link setup in umbraco
...and up to the client to pick wich kind of link they would like to use on the item they are creating.
And my code should do something like this:
An right now my code looks like this:
I can tell you when i'm doing something like this i'm getting my page Id
Hi Mads,
Try to put .Url on @myLink, and see if you get the link instead of the ID for the page.
I hope this helps you.
/Dennis
Hey Dennis
I've already been there :D
It just gives me an error :(
Error loading MacroEngine script (file: FrontpageFokusBoxs.cshtml)
Does anyone have another suggestion?
Hi Mads
First question, is this a Partial View Macro or are using using legacy Razor Macro? Next question, are you using MVC templates or WebForms?
Jeavon
Given you a using Umbraco v7, you should be using MVC templates or Partial View Macros with WebForms templates, therefore something Ike this:
Hi Jeavon Yes, i'm running full MVC. I'll try your suggestion :D
Right now i'm using Razor Macros on my views/mastertemplates?
Should I use Partial views Instead of Macros? and what is the big difference?
I'm still pretty new to running full MVC and Razor :D
My code look like this for now:
@inherits umbraco.MacroEngines.DynamicNodeContext @if(Model.AncestorOrSelf(2).Descendants("FocusBox").Count() > 0) { <div class="container row" id="focusBoxes"> @foreach(var Boxes in Model.AncestorOrSelf(2).Descendants("FocusBox")) { <div class="col-lg-3"> @if(Boxes.HasValue("focusImage")) { <img src="@Boxes.Media("focusImage", "umbracoFile")" width="100%" /> } <h3> <a href="#"> @if(Boxes.HasValue("header")) { @Boxes.header }else{ @Boxes.name } </a> </h3> @if(Boxes.HasValue("content")) { <p>@Boxes.content</p> } @{ var myLink = "#"; if(Boxes.HasValue("internalLink")) { myLink = Boxes.internalLink; } <a href="#">@myLink</a> } </div> } </div> }
And my content tree looks something like this
website.com
Frontpage
--Boxes
----Box 1
----Box 2
........
And every box has following properties:
header
image
content
internalLink
externalLink
mediaLink
targetBlank
And right now i've placed the racor macro at the frontpage view/template.
I can do this in XSLT but not i razor for the moment :o)
Hi Mads,
Yes, you should use Partial Views (for reuse) or Razor directly in your template. Partial View Macros should be used when you need parameters such as inserting a macro into the RTE (fairly rare use cases).
Try the following in either a Partial View or directly in your cshtml template:
This should work directly in the template, if you use a partial view, make sure the first line is
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
There is some great MVC documentation here and some excellent videos on umbraco.tv
Jeavon
I should add, Razor Macros using DynamicNode are legacy and should not be used for anything new.
Hi Jeavon
Finally it works :D
And it make totally sense with partial views vs razor macros.
I really dont know if this is the best solution but i works for me :D
Here is my finally code:
Great, that looks perfect to me :-)
Glad to hear, merry christmas and happy newyear to you my friend :D
Merry Christmas and Happy New Year to you too!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.