Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Jun 25, 2018 @ 07:43
    Matt
    0

    displaying document field with else command

    Morning all,

    Hope we all had good weekend, I'm looking for some help with the following;

    I have a policies page which works great if I just upload an attachment using the document picker, but what I'm looking to do now is give the user the option to either add an attachment or a link in the page and then displaying that option depending which field is filled in.

    Here is my code;

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = Model.Content.Site().FirstChild("policiesProdeduresAndGuidance").Children("ourPolicies")
                            .Where(x => x.IsVisible());
    }
    
    @foreach (var item in selection)
    {
    
        var documentPicker = item.GetPropertyValue<IPublishedContent>("documentPicker");
        var linkPicker = item.GetPropertyValue<IPublishedContent>("test");
    
        <div class="guidandpolicies-wrapper">
            <div class="w-row">
                <div class="w-col w-col-2">
                    @{
                        if (documentPicker != null)
                        {
                            <a href="@documentPicker.Url" target="_blank" class="document-link-block w-inline-block"><img src="~/images/policies_image.png"></a>
                        }else {
                                if (linkPicker != null)
                                {
                                <a href="@linkPicker.Url" target="_blank" class="document-link-block w-inline-block"><img src="~/images/policies_image.png"></a>
                                    }
                                }
                    }
                </div>
                <div class="w-col w-col-10">
                    <div class="guidanceandpolicies-info-div">
                        <h5 class="gnp-title">
                            @{
                                if (documentPicker != null)
                                {
                                    <a href="@documentPicker.Url" target="_blank" class="document-link-text">@item.GetPropertyValue("documentTitle")</a>
                                }else {
                                        if (linkPicker != null)
                                        {
                                            <a href="@linkPicker.Url" target="_blank" class="document-link-text">@item.GetPropertyValue("documentTitle")</a>
                                        }
                                }
                            }
                        </h5>
                        <div class="gnp-document-info">@item.GetPropertyValue("documentDescription")</div>
                    </div>
                </div>
            </div>
        </div>
    }
    

    My issue is if I add a document under documentPicker it shows correct, but what I want to do is if there is no documentPicker, but I want the field "linkPicker" to display. But I get nothing being outputted?

    Thanks in advance, Matt

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 14:09
    Frans de Jong
    1

    Hi Matt,

    First of all I would rewrite your code as follows for readability:

        if (documentPicker != null)
        {
           <a href="@documentPicker.Url" target="_blank" class="document-link-text">@item.GetPropertyValue("documentTitle")</a>
        }
        else if (linkPicker != null)
        {
           <a href="@linkPicker.Url" target="_blank" class="document-link-text">@item.GetPropertyValue("documentTitle")</a>
        }
    

    What datatype did you use for the linkpicker?

  • Matt 358 posts 841 karma points
    Jun 25, 2018 @ 14:50
    Matt
    0

    Hello,

    I'm used just a text box then validate it as a URL.

    Is this correct?

    Thanks

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 18:23
    Frans de Jong
    0

    Is the link internal or external?

    If the link is internal than I would use a content picker. If you use a content picker you can use the code you've shared.

    If the link is external you can use the textbox but the else if should look like this:

    else if(!string.IsNullOrEmpty(linkPicker)){
      <a href="@linkPicker" target="_blank" class="document-link-text">@linkPicker</a>
    }
    

    Mind you; this is not a solid solution because a external link without http(s):// wont work and the name of the link needs to be edited in a separate input if you want it to be something else than the url.

    I always use Multi url picker for managing internal and external links. Works like a charm!

  • Matt 358 posts 841 karma points
    Jun 26, 2018 @ 08:37
    Matt
    0

    Hello,

    I tried your code but I got the following;

    The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments

    Are you able to point me in the right direction.

    Thanks in advance, Matt

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 26, 2018 @ 08:39
    Frans de Jong
    100

    Sorry, you have to change this line to:

    var linkPicker = item.GetPropertyValue<IPublishedContent>("test");
    

    must be:

    var linkPicker = item.GetPropertyValue<string>("test");
    
  • Matt 358 posts 841 karma points
    Jun 26, 2018 @ 09:06
    Matt
    0

    Works a dream, thank you very much!

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 26, 2018 @ 09:09
    Frans de Jong
    0

    No problem.

    What you tried to do is cast a string to IPublishedContent. That's not going to work.

    Keep in mind my advice to look into multi url picker to prevent errors with bad content.

Please Sign in or register to post replies

Write your reply to:

Draft