Copied to clipboard

Flag this post as spam?

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


  • Mads Sørensen 188 posts 433 karma points
    Dec 13, 2013 @ 14:05
    Mads Sørensen
    0

    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:

    @*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>
    }
  • Mads Sørensen 188 posts 433 karma points
    Dec 13, 2013 @ 14:10
    Mads Sørensen
    0

    I can tell you when i'm doing something like this i'm getting my page Id

    @{
    var myLink = "#";
        if(Boxes.HasValue("internalLink"))
        {
        myLink = Boxes.internalLink;
        }
        <a href="#">@myLink</a>
    } 
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 13, 2013 @ 14:18
    Dennis Aaen
    0

    Hi Mads,

    Try to put .Url on @myLink, and see if you get the link instead of the ID for the page.

    @{
    var myLink ="#";
       
    if(Boxes.HasValue("internalLink"))
       
    {
        myLink
    =Boxes.internalLink;
       
    }
       
    <a href="#">@myLink.Url</a>
    }

    I hope this helps you.

    /Dennis

  • Mads Sørensen 188 posts 433 karma points
    Dec 13, 2013 @ 14:36
    Mads Sørensen
    0

    Hey Dennis 
    I've already been there :D

    It just gives me an error :(

    Error loading MacroEngine script (file: FrontpageFokusBoxs.cshtml) 

  • Mads Sørensen 188 posts 433 karma points
    Dec 16, 2013 @ 09:36
    Mads Sørensen
    0

    Does anyone have another suggestion? 

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

    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

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 22:26
    Jeavon Leopold
    0

    Given you a using Umbraco v7, you should be using MVC templates or Partial View Macros with WebForms templates, therefore something Ike this:

    @{ var myLink ="#";
         if(CurrentPage.HasValue("internalLink"))
         {
        myLink = Umbraco.Content(CurrentPage.internalLink).Url;
        }
        <a href="@myLink">mylink</a> }
    
  • Mads Sørensen 188 posts 433 karma points
    Dec 17, 2013 @ 22:41
    Mads Sørensen
    0

    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

  • Mads Sørensen 188 posts 433 karma points
    Dec 17, 2013 @ 22:55
    Mads Sørensen
    0

    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) 


  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 23:52
    Jeavon Leopold
    100

    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:

    @{
        var focusBox = CurrentPage.AncestorOrSelf(2).Descendants("FocusBox");
    }
    @if (focusBox.Any()){ 
        <div class="container row" id="focusBoxes">
        @foreach (var boxes in focusBox) { 
            <div class="col-lg-3">
                 @if (boxes.HasValue("focusImage"))
                 {
                     var focusImage = Umbraco.Media(boxes.focusImage);
                     <img src="@focusImage.Url" width="100%" />
                 } 
                <h3>
                    <a href="#">
                        @(boxes.HasValue("header") ? boxes.header : boxes.name) 
                    </a> 
                </h3> 
            @if (boxes.HasValue("content"))
            {
                <p>@boxes.content</p>
            } 
            @{
                var myLink = "#";
                if (boxes.HasValue("internalLink"))
                {
                    myLink = Umbraco.NiceUrl(boxes.internalLink);
                } 
                <a href="#">@myLink</a>
            } 
            </div> 
        } 
        </div> 
    }
    

    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

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 23:53
    Jeavon Leopold
    0

    I should add, Razor Macros using DynamicNode are legacy and should not be used for anything new.

  • Mads Sørensen 188 posts 433 karma points
    Dec 18, 2013 @ 10:28
    Mads Sørensen
    0

    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:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var focusBox = CurrentPage.AncestorOrSelf(2).Descendants("FocusBox");
    }
    
    @if (focusBox.Any()){ 
        <div class="container row" id="focusBoxes">
    
        @foreach (var boxes in focusBox) 
        { 
            <div class="col-lg-3">
    
                @if (boxes.HasValue("focusImage"))
                {
                    var focusImage = Umbraco.Media(boxes.focusImage);
                    <img src="@focusImage.Url" width="100%" />
                    }
    
                <h3>
                    <a href="#">
                        @(boxes.HasValue("header") ? boxes.header : boxes.name) 
                    </a> 
                </h3>
    
            @if (boxes.HasValue("content"))
            {
                <p>@boxes.content</p>
            }
    
            @{
                var myLink = "#";
                var target = "_self";
    
                if (boxes.HasValue("internalLink"))
                {
                    myLink = Umbraco.NiceUrl(boxes.internalLink);
                }          
                else if(boxes.HasValue("externalLink"))
                {
                myLink = boxes.externalLink;
                }
                else if(boxes.HasValue("mediaLink"))
                {
                myLink = Umbraco.Media(boxes.mediaLink).Url;
                }          
                if(boxes.HasValue("openNewWindow"))
                {
                target = "_blank";
                }          
                <a target="@target" href="@myLink">@myLink @boxes.openNewWindow </a>
            } 
       </div> 
        } 
        </div> 
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 10:48
    Jeavon Leopold
    0

    Great, that looks perfect to me :-)

  • Mads Sørensen 188 posts 433 karma points
    Dec 18, 2013 @ 10:55
    Mads Sørensen
    1

    Glad to hear, merry christmas and happy newyear to you my friend :D 

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 12:06
    Jeavon Leopold
    0

    Merry Christmas and Happy New Year to you too!

Please Sign in or register to post replies

Write your reply to:

Draft