Copied to clipboard

Flag this post as spam?

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


  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 02, 2013 @ 15:56
    Jon R. Humphrey
    0

    Help needed getting the url & title out of UrlPicker in an MVC Partial View?

    Hello all, 

    I have a doc type "Widget" which is loaded into the page using a partial view. Inside here is the option to add a button to the widget which links to either an outside page or internal content. I've set all this up as a new data-type and chosen XML as the output however when I try to get the url and title for said new "button" I can't seem to find the right "hook" to get this info out?

    Here's the code I have, please take a look and see where I'm making my mistake ... be honest, I can take it! ;-D

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    @using uComponents.DataTypes.UrlPicker
    @using uComponents.DataTypes.UrlPicker.Dto;
    @{
        Layout = null;
        var nodeIds = Model.GetPropertyValue<string>("WidgetPanels").Split(',');
        List<IPublishedContent> widgets = (from nodeId in nodeIds where !String.IsNullOrEmpty(nodeId) let publishedContent = Umbraco.NiceUrl(Convert.ToInt32(nodeId)) where !String.IsNullOrEmpty(publishedContent) && publishedContent != "#" select Umbraco.TypedContent(nodeId)).ToList();
    }
    @if( widgets.Any() )
    {
            foreach (var widget in widgets)
            {
                if (widget == null)
                {
                    continue;
                }
                var widgetTitle = string.Empty;
                string widgetButtonStyle;
    
                var widgetBg = widget.GetPropertyValue("widgetHasBackground").ToString() == "True" ? "bg-white" : "";
    
                if (!String.IsNullOrEmpty(widget.GetPropertyValue<string>("widgetTitle")))
                {
                    widgetTitle = String.Format("<h4>{0}</h4>", widget.GetPropertyValue<string>("widgetTitle"));
                }
                var widgetContent = widget.GetPropertyValue<string>("widgetContent");
                var widgetHasButton = widget.GetPropertyValue("widgetHasButton").ToString();
                var widgetButtonText = widget.GetPropertyValue<string>("widgetButtonText");
    
                switch (widget.GetPropertyValue("widgetButtonStyle").ToString())
                {
                    case "26":
                        widgetButtonStyle = "btn-blue";
                        break;
                    case "27":
                        widgetButtonStyle = "btn-red";
                        break;
                    default:
                        widgetButtonStyle = "btn-gray";
                        break;
                }
    @*HERE WE GO*@          
                UrlPickerState links = null;
                var widgetButtonAction = widget.GetPropertyValue("widgetButtonAction");
                if (widgetButtonAction != null)
                {
                    links = UrlPickerState.Deserialize( widgetButtonAction.ToString() );
                };
    
                var sectionClass = @widgetTitle.Contains("Wellbeing Portal") ? "pad-10 rounded-10 box-shadow-black-30 gradient-off_white" : "";
                <div class="sidebarwidget @widgetBg">
                    <div class="@sectionClass">
                        @Html.Raw(widgetTitle)
                        @Html.Raw(widgetContent)
                        @if (@widgetHasButton == "True")
                        {
                            if (links != null)
                            {
                                switch ((UrlPickerMode) links.Mode)
                                {
                                    case UrlPickerMode.Content:
                                    case UrlPickerMode.URL:
                                        <a class="button @widgetButtonStyle rounded-5 action" href="@links.Url" title="@(string.IsNullOrWhiteSpace(links.Title) ? widgetButtonText : links.Title)">@widgetButtonText</a>
                                        break;
                                }
                            }
    @*
    simple version which returns DynamicXML for the url:
                                <a class="button @widgetButtonStyle rounded-5 action" href="@widgetButtonAction">@widgetButtonText</a>
    *@
                        }
                    </div>
                </div>
            }
    }
    
    Finally, if there's any "better" way you think I could do any of this then please feel free to make comments about my code, I'm always open to learning!

    Thank you and HAPPY 2013!!!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 02, 2013 @ 16:06
    Lee Kelleher
    0

    Hi Jon,

    Do you know if the `widgetButtonAction` variable has a value before it is passed to the `UrlPickerState.Deserialize` call?

    I'm wondering if anything happens to the value that might cause the deserialisation to fail. Hmmm.

    Thanks, Lee.

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 02, 2013 @ 16:23
    Jon R. Humphrey
    0

    Lee,

    When I try to initialise as a string as follows:

    var widgetButtonAction = widget.GetPropertyValue<string>("widgetButtonAction");
    <p>@widgetButtonAction</p>

    this returns:

    Umbraco.Core.Dynamics.DynamicXml

    Does this help?
    J

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 02, 2013 @ 16:29
    Lee Kelleher
    0

    I haven't used the new DynamicXml object (yet) ... but I know that if you use `ToString()` it would output the full Type name(space). Which is no go for the `UrlPickerState.Deserialize` call.

    Taking a quick look at the core code (only because I have it to hand), I see there is a `ToXml()` method... which might work?

    UrlPickerState links = null;
    var widgetButtonAction = widget.GetPropertyValue("widgetButtonAction").Value;
    if (widgetButtonAction != null)
    {
        links = UrlPickerState.Deserialize( widgetButtonAction.ToXml() );
    };

    Cheers, Lee.

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 02, 2013 @ 16:31
    Jon R. Humphrey
    1

    Lee, et al:

    I continued with messing about with this and got it to work, but it seems to be a bit of a hack, not sure why this wouldn't be the same as before?

    This is the new snippet that works:

               UrlPickerState links = null;
                var widgetButtonAction = widget.GetProperty("widgetButtonAction").Value;
    
                if (widgetButtonAction != null)
                {
                    links = UrlPickerState.Deserialize( widgetButtonAction.ToString() );
                };
    
    [...]
                        @if (@widgetHasButton == "True")
                        {
                            if (links != null)
                            {
                                switch (links.Mode)
                                {
                                    case UrlPickerMode.Content:
                                    case UrlPickerMode.URL:
                                        <a class="button @widgetButtonStyle rounded-5 action" href="@links.Url" title="@(string.IsNullOrWhiteSpace(links.Title) ? widgetButtonText : links.Title)">@widgetButtonText</a>
                                        break;
                                }
                            }
    

    But this is only different in the way the value of widgetButtonAction is retrieved: GetPropertyValue vs. GetProperty(Model).Value? 

    Thoughts?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 02, 2013 @ 16:35
    Lee Kelleher
    0

    Yes, there seems to be subtle differences between `GetPropertyValue` and `GetProperty(Model).Value`.  I think the `GetPropertyValue` gets the string value, whereas the `GetProperty` runs through the `IPropertyEditorValueConverter` code, (for strongly-typed objects).

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 02, 2013 @ 16:59
    Jon R. Humphrey
    1

    Lee,

    In response, and to help anyone else, the "ToXML" method doesn't show in the VS intellisense and I've checked it against the original code which then breaks the code even further:

    CS1928: 'object' does not contain a definition for 'ToXml' and the best extension method overload 'umbraco.NodeExtensions.ToXml(umbraco.NodeFactory.Node)' has some invalid arguments

    So I think I'll go with my solution unless anyone else shows us a better way!

    Final version:

            UrlPickerState links = null;
           var widgetButtonAction = widget.GetProperty("widgetButtonAction").Value;
     
    if (widgetButtonAction != null)
            {
            links = UrlPickerState.Deserialize( widgetButtonAction.ToString() );
            };
     
            var sectionClass = @widgetTitle.Contains("Wellbeing Portal") ? "pad-10 rounded-10 box-shadow-black-30 gradient-off_white" : "";
            <div class="sidebarwidget @widgetBg">
            <div class="@sectionClass">
            @Html.Raw(widgetTitle)
            @Html.Raw(widgetContent)
            @if (@widgetHasButton == "True")
            {
        if (links != null)
            {
            switch (links.Mode)
            {
            case UrlPickerMode.Content:
    case UrlPickerMode.URL:
            <a class="button @widgetButtonStyle rounded-5 action" href="@links.Url" title="@(string.IsNullOrWhiteSpace(links.Title) ? widgetButtonText : links.Title)">@widgetButtonText</a>
            break;
            }
            }
            }
            </div>
            </div>
     

    Thanks for all the help!

    Jon

  • Funka! 398 posts 661 karma points
    Aug 07, 2013 @ 05:15
    Funka!
    0

    Don't forget to add

    @using uComponents.DataTypes.UrlPicker
    @using uComponents.DataTypes.UrlPicker.Dto;
    

    to the top of your partial view!

    I also wanted to say, thank you for the reference! Getting to these properties via UrlPickerState.Deserialize(myTypedNode.GetProperty("myLinkPickerProp").Value.ToString()).Url etc. isn't the easiest thing to remember or type out, but does seem to be the shortest route. (Of course, you'll probably want to check for null like Jon did, this was only me regurgitating for posterity!)

    Thanks again!

  • firepol 125 posts 173 karma points
    May 13, 2014 @ 14:03
    firepol
    0

    For who encounters the same issue, hope this helps.

    In my case, at the top of my rator view I have:

    @using uComponents.DataTypes.UrlPicker.Dto
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

     This works for me without any deserialize. I use "XML" for the UrlPicker, tested on umbraco 6.2.0.

    UrlPickerState link = (UrlPickerState)content.GetProperty("myUrlPickerProperty").Value;

    But I don't use the code above in my view. I use a helper method to generate the HTML link, as somebody in a previous post shared:

    public static string GetUrlPickerLinkHTML(IPublishedContent content, string propertyName)
    {
    UrlPickerState link = (UrlPickerState)content.GetProperty(propertyName).Value;
    string linkHTML = string.Format("{2}",
    link.Url, link.NewWindow ? " target=\"_blank\"" : "",
    string.IsNullOrEmpty(link.Title) ? link.Url : link.Title);

    return linkHTML;
    }

    So in the view, to get the linkHTML and render it in the view:

     @Html.Raw(MyHelper.GetUrlPickerLinkHTML(Model.Content, "myUrlPickerProperty")) 
Please Sign in or register to post replies

Write your reply to:

Draft