Copied to clipboard

Flag this post as spam?

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


  • Łukasz Koruba 30 posts 151 karma points
    Jan 26, 2017 @ 14:20
    Łukasz Koruba
    1

    Macro parameter Json to model

    Hello,

    I've got this piece of code

        @{
        var multiUrlPicker = Model.MacroParameters["linkPicker"];
        <p>@multiUrlPicker</p>
        }
    

    The ouput of that is enter image description here

    How can I get json to a model? Is there something in umbraco or need to write some custom code to deserialize it?

  • Łukasz Koruba 30 posts 151 karma points
    Jan 26, 2017 @ 14:38
    Łukasz Koruba
    1

    So i've got this code and works correctly but is there a better way of doing it?

    @{
        var pickerParameter = Model.MacroParameters["linkPicker"];
        var multiUrls = new MultiUrls(pickerParameter.ToString());
        Link firstUrl = null;
    
        if (multiUrls.Any())
        {
            firstUrl = multiUrls.First();
        }
    }
    @{
        if (firstUrl != null)
        {
            <a href="@firstUrl.Url">@firstUrl.Name</a>
        }
    }
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 26, 2017 @ 14:50
    Alex Skrypnyk
    0

    Hi

    Try to use this code:

    var data = JsonConvert.DeserializeObject(value.ToString());
    
    public class LinkPickerModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Target { get; set; }
        public string Hashtarget { get; set; }
    }
    

    Hope it will help you.

    Thanks,

    Alex

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 26, 2017 @ 14:51
    Alex Skrypnyk
    0

    Are you using "Multi Url Picker" or "Link Picker"?

  • Łukasz Koruba 30 posts 151 karma points
    Jan 26, 2017 @ 14:52
    Łukasz Koruba
    0

    It's Multi Url Picker.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 26, 2017 @ 15:24
    Alex Skrypnyk
    0

    You can simplify these lines:

        Link firstUrl = null;
    
        if (multiUrls.Any())
        {
            firstUrl = multiUrls.First();
        }
    

    To:

        var firstUrl = multiUrls.FirstOrDefault();
    

    Thanks,

    Alex

  • Łukasz Koruba 30 posts 151 karma points
    Jan 27, 2017 @ 08:54
    Łukasz Koruba
    0

    Yeah, I know. But I'm wondering if there is a better way of casting macro parameter to Multi Url Picker model.

Please Sign in or register to post replies

Write your reply to:

Draft