Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 13:15
    Kate
    0

    Passing a Parameter Variable in razor

    Hi

    I have made a macro with Parameter "propNameRazor"

    How can I insert "propNameRazor" instead of "link3"?

    @{
    string target = Model.link3.NewWindow ? " target=\"_blank\"" : "";
    <a href="@Model.link3.Url" title="@Model.link3.Title" target="@target">
    @Model.link3.Title
    </a>
    }

    Thanks in advance

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 13:36
    Jeavon Leopold
    0

    Hi Kate,

    "link3" must be the alias of your property in your document type, so you would need to update the property in the document type?

    Is this property using a uComponents URLPicker property editor?

    Jeavon

  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 13:48
    Kate
    0

    Hi Jeavon

    "link3" is the alias of my property in my document type :-)

    And yes, im using the uComponents URLPicker :-)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 13:55
    Jeavon Leopold
    0

    Ok, so if you want your code to be:

    @{
    string target = Model.propNameRazor.NewWindow ? " target=\"_blank\"" : "";
    <a href="@Model.propNameRazor.Url" title="@Model.propNameRazor.Title" target="@target">
    @Model.propNameRazor.Title
    </a>
    }
    

    Just change the alias of the property in your document type to "propNameRazor"

    Jeavon

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 14:02
    Jeavon Leopold
    0

    You may also need to @Html.Raw your linkTarget.

    This is the code sample we use for uComponents Razor Macro (without Model Binding)

    @{
        if (Model.propNameRazor != null && !String.IsNullOrEmpty(Model.propNameRazor.Url))
        {
            var linkTarget = (Model.propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty;
            <a href="@Model.propNameRazor.Url" class="someCssClass" @Html.Raw(linkTarget)>@Model.propNameRazor.Title</a>
        }
    }
    
  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 14:16
    Kate
    0

    It still won't work.

    When I write "propNameRazor" insted of "link3" it does not do anything. So do I have to declare the macro parameter in the script file?

    Something like this:

    var propNameRazor = Parameter.propNameRazor;

     

  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 14:30
    Kate
    0

    in my template i have this:

    <umbraco:Macro propNameRazor="link3" Alias="RazorPicker" runat="server"></umbraco:Macro>


    In my script file, when I write this:

    var propNameRazor = Parameter.propNameRazor;
    <p>@propNameRazor</p>

    it spits out "link3"

    So I know that there is a value from the parameter. I just dont know how to conect my variable "propNameRazor" with this line:

    string target =Model.link3.NewWindow?" target=\"_blank\"":"";

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 14:36
    Jeavon Leopold
    0

    Ah I see, try this:

    @{
        if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor))
        {
            dynamic propNameRazor = Model.GetPropertyValue(Parameter.propNameRazor);
    
            if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url))
            {
                var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty;
                <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a>
            }
        }
    }
    
  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 14:42
    Kate
    0

    it gave my an error

    If I just write this

    dynamic propNameRazor = Model.GetPropertyValue(Parameter.propNameRazor);
    <p>@propNameRazor </p>

    This is what comes out on the page:

    <url-picker 
    mode="Content"><new-window>False</new-window><node-id>1094</node-id><url>/uk/engelske-underside.aspx</url><link-title>Det
     er den jeg skal bruge</link-title></url-picker> 

     

  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 14:47
    Kate
    0

    It seems that it does not like this if-sentenst

    if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url))
    {
    var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty;
    <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a>
    }
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 15:28
    Jeavon Leopold
    0

    Ah yes, you will need:

    dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor));
    

    and add @using umbraco.MacroEngines at the top

  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 15:32
    Kate
    0

    Like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines

    @{
    if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor))
    {
    dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor));

    if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url))
    {
    var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty;
    <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a>
    }
    }
    }
  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 15:38
    Kate
    0

    Sorry, it didn't work.

    can it be something with my Parameter. It's Type is set to "Text". Should that be something else?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 16:09
    Jeavon Leopold
    100

    Wow, it's more complicated that I remember, this seems to be working:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor))
        {               
            dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor));
    
            if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.url))
            {
                var linkTarget = (propNameRazor.BaseElement.Element("new-window").Value == "True") ? " target=\"_blank\"" : string.Empty;
                <a href="@propNameRazor.url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.BaseElement.Element("link-title").Value</a>
            }
        }
    }
    
  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 17:04
    Kate
    0

    Jubii, it works

    You're my favorite person of the week :-)

    Thanks for your time and patience

    /Kate

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

    You're welcome and have a great weekend.

    Depending on the version of uComponents you are using, you maybe able to simplify this a lot to:

    @using uComponents.DataTypes.UrlPicker.Dto
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor))
        {
            var urlPicker = UrlPickerState.Deserialize(Model.GetPropertyValue(Parameter.propNameRazor));
    
            if (urlPicker != null && !String.IsNullOrEmpty(urlPicker.Url))
            {
                var linkTarget = (urlPicker.NewWindow) ? " target=\"_blank\"" : string.Empty;
                <a href="@urlPicker.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@urlPicker.Title</a>
            }
        }
    }
    
  • Kate 267 posts 610 karma points
    Jan 17, 2014 @ 18:03
    Kate
    0

    That works great too and it's easier to read :-)

    Have a nice weekend

Please Sign in or register to post replies

Write your reply to:

Draft