Copied to clipboard

Flag this post as spam?

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


  • Chizzl 36 posts 107 karma points
    Jul 22, 2011 @ 17:02
    Chizzl
    0

    How to work with the Urlpicker datatype in razor

    Hi,

    I'm trying to display the url from the urlpicker datatype, but no luck so far.  Does somebody know how to call the url with razor?

    @item.partnerUrl

    generates 

    URL,True,,http://www.domain.tld,link

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 26, 2011 @ 15:23
    Ismail Mayat
    0

    kris,

    i recently did it and i have

    @if (Model.usefulLinks.ToString() != string.Empty)

    {

        <ul>

        @foreach (var item in Model.usefulLinks.BaseElement.Elements("link"))

        {

            if (item.Attribute("type").Value == "internal")

            {

                <li><a href="@umbraco.library.NiceUrl(int.Parse(item.Attribute("link").Value))">@item.Attribute("title").Value</a></li>

            }

            else { 

                <li><a href="@item.Attribute("link").Value" rel="external">@item.Attribute("title").Value</a></li>

            }                                                                                                                                                                                                                                                                                                                   

        }  

        </ul>

    }

    Regards

    Ismail

     

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 11:22
    Anthony Candaele
    0

    Has anyone yet used the uComponent Url Picker with v6 Razor

     

    I'm trying this:

    @Model.Content.GetProperty("mediaArticleLink").Url

    But I'm getting this error by VS:

    Error226'Umbraco.Web.Models.PartialViewMacroModel' does not contain a definition for 'mediaArticleLink' and no extension method 'mediaArticleLink' accepting a first argument of type 'Umbraco.Web.Models.PartialViewMacroModel' could be found (are you missing a using directive or an assembly reference?)

     

    Thanks for your help,

    Anthony

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 13:54
    Ali Sheikh Taheri
    1

    create a c# class in app_code or if you have your website as web application you can put the below file into any folder you want. (remember to build the solution after adding this class)

    using System.Collections.Generic;
    using System.Web;
    
    using uComponents.DataTypes.MultiUrlPicker.Dto;
    using uComponents.DataTypes.UrlPicker;
    using uComponents.DataTypes.UrlPicker.Dto;
    
    /// <summary>
    /// Global functions static partial class
    /// </summary>
    public static partial class GlobalFunctions
    {
        public static class uComponents
        {
            #region uComponent extensions
    
            public static IHtmlString GetUrlPickerAsLinkTag(string urlPickerState, string cssClass = "", string anchorLink = "", string iconCssClass = "")
            {
                var urlPicker = UrlPickerState.Deserialize(urlPickerState);
    
                if (string.IsNullOrWhiteSpace(urlPickerState) || urlPicker.Url == null)
                {
                    return new HtmlString(string.Empty);
                }
    
                return GetUrlPickerAsLinkTag(urlPicker, cssClass, anchorLink, iconCssClass);
            }
    
            public static IHtmlString GetUrlPickerAsLinkTag(UrlPickerState urlPicker, string cssClass = "", string anchorLink = "", string iconCssClass = "")
            {
                cssClass = string.IsNullOrEmpty(cssClass) ? string.Empty : "class=\"" + cssClass + "\"";
    
                var newWindow = urlPicker.NewWindow ? "target=\"_blank\"" : string.Empty;
    
                // If external url and not null
                if (urlPicker.Mode == UrlPickerMode.URL)
                {
                    // If without "http://" then add it
                    if (!urlPicker.Url.ToLower().StartsWith("http://"))
                    {
                        urlPicker.Url = "http://" + urlPicker.Url;
                    }
                }
    
                if (!string.IsNullOrWhiteSpace(anchorLink))
                {
                    urlPicker.Url = "#" + anchorLink;
                }
    
                if (!string.IsNullOrWhiteSpace(iconCssClass))
                {
                    iconCssClass = string.Format("<span class=\"{0}\"></span>", iconCssClass);
                }
    
                return
                    new HtmlString(
                        string.Format(
                            "<a href=\"{0}\"  {1} {2}>{3}{4}</a>", urlPicker.Url, cssClass, newWindow, urlPicker.Title, iconCssClass));
            }
    
            public static MultiUrlPickerState ConvertMultiUrlPickerToList(string multiUrlPicker)
            {
                return MultiUrlPickerState.Deserialize(multiUrlPicker);
            }
    
            #endregion
        }
    }
    

    then within the page call it like this:

    @GlobalFunctions.uComponents.GetUrlPickerAsLinkTag(item.partnerUrl.ToString(), "myCssClass")
    

    this is a very generic class which can be used in any solution/project and that's what I do almost in all the projects.

    hope this will help you all.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 14:00
    Ali Sheikh Taheri
    1

    I know there might be some shorter code but the above code will cover every scenario including Multi Url Picker.

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 14:10
    Anthony Candaele
    0

    Hi Ali,

    Thanks again for the code. This does indeed seem a lot of code just to get some values out of the uComponents Url Picker.

    Eric Schrepel suggested this:

    @Model.urlPicker.Mode
    @Model.urlPicker.Title@*not linktitle as previously reported *@
    @Model.urlPicker.Url
    @Model.urlPicker.NodeId
    @Model.urlPicker.NewWindow

    but I tried that, and it doesn't seem to work in v6 Razor (see my post)

    greetings,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 14:15
    Anthony Candaele
    0

    @Ali,

    I used to get my values out of the Url Picker like this:

    if (Model.mediaArticleLink.Title != null)

                    {                    

                        <a href="@Model.mediaArticleLink.Url" title="@Model.mediaArticleLink.Title" target="_blank">

                            @Model.mediaArticleLink.Title

                        </a>

                    }

                    else{                    

                        <a href="@Model.mediaArticleLink.Url" target="_blank">@Model.mediaArticleLink.Url</a>

                    }

     

    but this doesn't seem to work anymore in v6 Razor

     

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 14:17
    Ali Sheikh Taheri
    1

    with Umbraco 6 you cannot use model as it is strongly Typed so you need to use CurrentPage which is dynamic

    @CurrentPage.urlPicker.Mode
    @CurrentPage.urlPicker.Title
    @CurrentPage.urlPicker.Url
    @CurrentPage.urlPicker.NodeId
    @CurrentPage.urlPicker.NewWindow
    
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 14:21
    Anthony Candaele
    0

    Yeah, I already tried that, but then I'm getting a RuntimeBinderException:

    "Umbraco.Core.Dynamics.DynamicXml' does not contain a definition for 'Title"

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 14:31
    Ali Sheikh Taheri
    0

    I am assuming that your property alias is mediaArticleLink

    so now try this:

        if(CurrentPage.mediaArticleLink.Any)
        {
            var myUrlPicker = CurrentPage.mediaArticleLink.First;
            @myUrlPicker.Mode
            @myUrlPicker.Title
            @myUrlPicker.Url
            @myUrlPicker.NodeId
            @myUrlPicker.NewWindow
        }
    
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 14:50
    Anthony Candaele
    0

    does'nt work, I checked 

    var myUrlPicker = CurrentPage.urlPicker.First;

    in the debugger and it doesn't generate an error but it also doesn't return anything. myUrlPicker is just an empty variable.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 15:22
    Ali Sheikh Taheri
    0

    i've updated the code, is urlPicker your property alias?

    if yes ,it is so odd I've tried the above code and it works fine on umbraco 6.0.5.

    could you tell me which version of umbraco you are using?

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 15:31
    Anthony Candaele
    0

    the property alias is:

    mediaArticleLink

    my Umbraco version is 6.1.5

    I'm using the MVC mode

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 15:47
    Ali Sheikh Taheri
    0

    are you using xml mode or csv?

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 15:50
    Anthony Candaele
    0

    the Data mode is xml

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 15:55
    Ali Sheikh Taheri
    0

    I think that's the problem is there any chance you could convert it to CSV?

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 16:04
    Ali Sheikh Taheri
    0

    try this

    @CurrentPage.mediaArticleLink.url
    

    url has to be small case not capital. and you dont need to change xml to csv

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:04
    Anthony Candaele
    0

    I changed the datatype configuration to CSV mode, but that didn't solve it.

    The link I stored with the Url Picker looks like this in umbraco.config

    <mediaArticleLink>

                  <url-picker mode="URL">

                    <new-window>False</new-window>

                    <node-id />

                    <url>www.umbraco.com</url>

                    <link-title>testlink</link-title>

                  </url-picker>

    </mediaArticleLink>

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:11
    Anthony Candaele
    0

    alas, @CurrentPage.mediaArticleLInk.url isn't working either

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 16:14
    Ali Sheikh Taheri
    0

    it is so odd, I've just tried that with xml mode on umbraco 6.1.5 and it worked fine.

    are you sure you are not missing anything? what error do you get?

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:23
    Anthony Candaele
    0

    when I run this code:

     

    <a href="@CurrentPage.mediaArticleLink.url" title="@CurrentPage.mediaArticleLink.title" target="_blank">

               @CurrentPage.mediaArticleLink.title

     </a>

     

    I get a RuntimeBinderException:

    'Umbraco.Core.Dynamics.DynamicXml' does not contain a definition for 'title'

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:26
    Anthony Candaele
    0

    mystery solved, I ran this code:

    <a href="@CurrentPage.mediaArticleLink.url" title="" target="_blank">

               @CurrentPage.mediaArticleLink.url

            </a>

    and now the link is showing.

    So it was @CurrentPage.mediaArticleLink.title that was generating the error.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 16:34
    Ali Sheikh Taheri
    1

    oh man, so it's not complaining about url, it's the title. because the title is called link-title and a field doesn't accept dash (in c#) so you have to get it like this:

    @CurrentPage.mediaArticleLink.XPath("link-title").InnerText
    

    I am sure this is your answer

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:39
    Anthony Candaele
    1

    Indeed

    @CurrentPage.mediaArticleLink.XPath("//link-title").InnerText

    worked!

    Thanks a lot for your patience.

    In a tweet, Lee Kelleher stated that the uComponents Link Picker will be added to the Umbraco Core. I hope there will be some documentation about it then.

    have a nice weekend,

    Anthony

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 16:44
    Ali Sheikh Taheri
    2

    Is there anyway you could mark it as solution?

    have a great weekend.

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 16:44
    Anthony Candaele
    1

    for everyone struggling with the same issue, this was the solution:

    <a href="@CurrentPage.mediaArticleLink.url" title="@CurrentPage.mediaArticleLink.XPath("//link-title").InnerText" target="_blank">

               @CurrentPage.mediaArticleLink.XPath("//link-title").InnerText

     </a>

    With lot's of thanks and appreciation to Ali Sheikh Taheri for his help and patience!

Please Sign in or register to post replies

Write your reply to:

Draft