Copied to clipboard

Flag this post as spam?

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


  • Yuxuan Li 12 posts 122 karma points
    Jun 29, 2016 @ 01:52
    Yuxuan Li
    1

    how to use url picker in nested content?

    Hi All,

    I'm new to Umbraco world. Currently I'm using nested content in my project, this is an amazing plugin. Now I have a question about how to use url picker in nested content?

    Here is my code (but always show me the error:'object' does not contain a definition for 'url' and no extension method 'url' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @{
         var items = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("nestedItem");
         foreach (var item in items)
        {
            var image = item.GetPropertyValue("itemImage");
            var UrlPicker = item.GetPropertyValue("itemUrl");
            if (image != null)
            {
                <div class="col-xs-6 col-md-3">
    
                <img src="@Umbraco.Media(image).UmbracoFile" alt="">
                    <h3>
                        @Umbraco.Field(item, "itemTitle")
                    </h3>
                    <p>
                        @Umbraco.Field(item, "itemText")
                    </p>     
                    <a href="@UrlPicker.url" target="#" style="text-align: center; margin: 20px;"><h2 style="text-align:center">CONTACT US TODAY</h2></a>                      
            </div>
           }
         }
       }
    

    when I run this code, the images, texts was able to works correctly, but the url is working wrong. Did I made some mistake in the code? Can anyone help me to check or give me some idea how does the url picker work in nested content?

    Thanks very much!

  • David Peck 587 posts 1553 karma points c-trib
    Jun 29, 2016 @ 07:33
    David Peck
    1

    UrlPicker returns a collection of links, even if only one is selected. I'm guessing something like this will work:

    @UrlPicker.First(u => u.IsValid()).Url
    
  • David Peck 587 posts 1553 karma points c-trib
    Jun 29, 2016 @ 08:02
    David Peck
    0

    Michaël is right. My answer is only valid if you're using this: https://our.umbraco.org/projects/backoffice-extensions/multi-url-picker/

  • Yuxuan Li 12 posts 122 karma points
    Jun 30, 2016 @ 00:24
    Yuxuan Li
    0

    Thanks very much David.

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Jun 29, 2016 @ 07:36
    Michaël Vanbrabandt
    101

    Hi Yuxuan Li,

    if your url picker is a content picker then the value of UrlPicker will be the Id of the selected node.

    You can get the url of the node by using:

    @Umbraco.NiceUrl(UrlPicker)
    

    or by getting the node:

    @Umbraco.Content(UrlPicker).Url
    

    /Michaël

  • Yuxuan Li 12 posts 122 karma points
    Jun 30, 2016 @ 00:25
    Yuxuan Li
    0

    Thanks Michael, it works great!

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Jun 30, 2016 @ 06:21
    Michaël Vanbrabandt
    0

    Hi Yuxuan,

    Glad that I could help you out!

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft