Copied to clipboard

Flag this post as spam?

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


  • Phillip Turner 98 posts 412 karma points
    May 16, 2016 @ 13:51
    Phillip Turner
    0

    Link Picker in a Partial View.

    I just downloaded the external package Link Picker and I am trying to use in in a partial view but am having a difficult time.

    Here is the code for my Partial:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{ 
    var pills = Model.Content.Children.Where(c => c.DocumentTypeAlias.Equals("aboutPanel"));
    int _s2 = 2;
    int _s4 = 4;
    int _s2count = 0;
    int _s4count = 0;
    
    <div class="container-fluid no-side-padding" style="padding-top: 20px;">
    <div class="container">
        <div class="row">
            @foreach (var pill in pills)
            {
                _s2count++;
                _s4count++;
            // This is where the Link Picker URL needs to go
            <div class="col-sm-6 col-md-3 col-lg-3 about-us-panel" onclick="location='@pill["panelURL"]';">
                <div class="hovereffect img-rounded">
                    <img class="img-responsive img-about img-rounded" src="@Umbraco.Media(pill["aboutImage"].ToString()).Url" />
                    <div class="overlay img-rounded"><h2>@pill["panelTitle"]</h2></div>
                </div>
                <div class="clearfix"></div>
            </div>
                if (_s2count == _s2 && _s4count == _s4)
                {
                    _s2count = 0;
                    _s4count = 0;
                   <div class="clearfix visible-sm visible-md visible-lg"></div>
                }
                else if (_s2count == _s2)
                {
                    _s2count = 0;
                    <div class="clearfix visible-sm"></div>
                }
            }
        </div>
    </div>
    </div>
    }
    

    The problem I have is Link Picker returns json and I am not too sure how to access the "url', "name", "target" properties of Link Picker.

    The instructions use CurrentPage as an example and use the following syntax:

    @CurrentPage.LinkPickerName.url
    

    This does not work for me as I access the document type properties like this:

    @pill["datatype"]
    

    I have tried a number of different combinations but all are fruitless. I even tried DeSerializing the JSON object into dynamic but was getting some null errors.

    HELP!!!!

    Any suggestions?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 16, 2016 @ 16:08
    Alex Skrypnyk
    100

    Hi Phillip,

    Try to add new class to your solution -

    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; }
        }
    

    And after that you will be able to use deserialization:

                    var linkPicker = JsonConvert.DeserializeObject<LinkPickerModel>(block.GetPropertyValue<string>("link"));
    
                    if (linkPicker != null)
                    {
                        <a href="@linkPicker.Url" target="@linkPicker.Target" class="link">@linkPicker.Name<span></span></a>
                    }
    

    Cheers

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    May 16, 2016 @ 18:54
    Dan Diplo
    0

    There's more instructions on the GitHub site: https://github.com/Gibe/Umbraco-Link-Picker

    But really the project could do with writing a property value converter so the conversion is done automatically. Should be relatively simple to write one.

  • Phillip Turner 98 posts 412 karma points
    May 16, 2016 @ 19:16
    Phillip Turner
    0

    I looked up the GitHub details. Not too much help. I will Alex's suggestion.

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 16, 2016 @ 19:55
    Bjarne Fyrstenborg
    0

    The Github readme https://github.com/Gibe/Umbraco-Link-Picker/blob/master/README.md is mentioning exactly what Alex suggest. But the package could include the compiled assembly, with use of PropertyValueConverter and the model, so you just need to add e.g. using Gibe.LinkPicker.Models.. Like in Archetype where you add using Archetype.Models and using Archetype.Extensions

    /Bjarne

  • Phillip Turner 98 posts 412 karma points
    May 17, 2016 @ 13:57
    Phillip Turner
    0

    Thanks for everyone help. This is what I ended up doing.

        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; }   
    
        public LinkPickerModel ReturnModel(string JsonObject)
        {
            var linkpicker = JsonConvert.DeserializeObject<LinkPickerModel>(JsonObject);
    
            return (LinkPickerModel)linkpicker;
        }
    }
    

    The Partial View:

    LinkPickerModel ipm;
    
    foreach (var pill in pills)
    {
        lpm = new LinkPickerModel().ReturnModel(pill["panelURL"].ToString());
    }
    <div>@lpm.Url</div>
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 17, 2016 @ 14:11
    Alex Skrypnyk
    0

    Great Phillip !

Please Sign in or register to post replies

Write your reply to:

Draft