Copied to clipboard

Flag this post as spam?

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


  • mmmoustache 50 posts 79 karma points
    May 22, 2012 @ 16:55
    mmmoustache
    0

    uComponents Multi-URL Picker - problems with outputted value

    Hi Everyone,

    I'm using a multi-url picker with Umbraco 4.7.1.1 and I'm trying to check whether or not the property is empty, and if it is empty, hide it's container div; should be fairly straight forward, but my problem is that when no nodes are selected, the property still has a value? I've tried various methods to no avail, but on some pages they work and some they don't. It seems that pages with this property which have had a node selected - then later removed - have a different value to pages that have never used this property.

    Here's a few methods I've tried doing and as you can see, even trying them all together doesn't work; this code works somewhat, but the container div still shows on some pages: 

    @if((Model.multiUrlPicker.GetType(== typeof(DynamicXml)|
          (Model.HasProperty("multiUrlPicker"&Model.GetProperty("multiUrlPicker").Value != "<multi-url-picker />"|
          (Model. multiUrlPicker.InnerText != ""|
          (Model.HasProperty("multiUrlPicker"&Model.GetProperty("multiUrlPicker").Value != ""))
    {
    @* Show container & list items *@
    }else{
    @* Don't show them *@
    }

    Are there any other methods to check whether or not this property is empty? 


    Kind Regards,
    mmmoustache

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2012 @ 17:23
    Tom Fulton
    1

    Hi,

    Personally I like to use a helper method to cast the value to MultiUrlPickerState - makes things nice and simple with a strongly typed object to work with.  Here is the helper function I use (placed in App_Code):

     

    public static MultiUrlPickerState GetMultiUrlPickerState(string value)
    {
    MultiUrlPickerState state = null;
    if (!string.IsNullOrEmpty(value))
    {
    state = MultiUrlPickerState.Deserialize(value);
    }
    return state;
    }

    Then to use it:

     

    MultiUrlPickerState selectedLinks = GlobalHelpers.GetMultiUrlPickerState(Page.Model.GetProperty("selectedLinks").Value);
    if (selectedLinks.Items.Any())
    {
     <div class="container">
      @foreach (var link in selectedLinks.Items) {
        <a href="@link.Url">link</a>
      } 
     </div>
    }

    Note this will happen automatically in uComponents v4 via RazorDataTypeModels - but this is a good interim solution.

    HTH,
    Tom 

     

     

  • mmmoustache 50 posts 79 karma points
    May 22, 2012 @ 18:06
    mmmoustache
    0

    Hi Tom,

    Thanks a lot for the info, just a quick question though: what file format does the helper function needs to be in? .cs?

    Kind Regards,
    mmmoustache 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2012 @ 18:10
    Tom Fulton
    0

    Yep, just add a .cs file to your App_Code folder, for example MyHelpers.cs.  Then add the block above wrapped in @functions { }.  Then you can access it from any Razor file by MyHelpers.GetMultiUrlPickerState

Please Sign in or register to post replies

Write your reply to:

Draft