Copied to clipboard

Flag this post as spam?

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


  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 14:49
    Kieron
    0

    Where String Contains String

    Hi guys, got this:

    if(item.HasValue("location") && pagesID.Contains(adPlace)){
    

    @pagesID could be something like this

    "1111 3232 4564 2132" 
    

    And @adPlace would be like this

    3232
    

    Can't seem to get the above to work.

    I get this;

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

    Thinking about it, strings is probably not the best way to do it, im only comparing Page ID's afterall!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 14, 2018 @ 14:54
    Jan Skovgaard
    100

    Hi Kieron

    From the top of ny head I would say that you will need to write it as

    if(item.HasValue("location") && pagesID.Contains(adPlace.ToString()))
    

    Does this work?

    /Jan

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 15:00
    Kieron
    0

    I think this works, its showing the page atleast!

    I don't think im getting matches though, could it be the way I build my long string up?

    string pagesID = "";

     @{
            foreach(var item in Model.Content.Ancestors().OrderBy(x => x.Level))
                {
                    pagesID = string.Concat(@item.Id.ToString(),"  ",pagesID);
                }
                    pagesID = string.Concat(@Model.Content.Id.ToString(),"  ",pagesID);
             }
    

    I added spaces so that the ID's are seperate.

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 15:41
    Kieron
    0

    Sorry Jan, would you change this at all if rather than comparing 1232 1212 3273 to 9383, but if it was multiple to multiple, like

    1231 5463 2435
    

    Contains

    2344 1231 4573
    

    Ive changed to be a multinode picker now, and when I pick one item it works, like the original question, but if i pick multiples it doesnt.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 14, 2018 @ 16:00
    Jan Skovgaard
    0

    Hi Kieron

    What is it exactly you're trying to achieve and what does your full amended code look like? And is it giving you a new error message or is it the same as from the original question?

    /Jan

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 16:03
    Kieron
    0

    Im not getting an error, but now im not comparing '1121' to '3423 1231 1121' but rather longer strings to longer strings, so im getting a result of none basically.

    Here is my code

    @{
    string pagesID = "";
    string adPlace = "";
    var selection = Umbraco.TypedContent(1384).Children("headerAdvert").Where(x => x.IsVisible() && x.GetPropertyValue<bool>("active"));
        foreach(var item in Model.Content.Ancestors().OrderBy(x => x.Level))
            {
                pagesID = string.Concat(@item.Id.ToString(),"  ",pagesID);
            }
                pagesID = string.Concat(@Model.Content.Id.ToString(),"  ",pagesID);
                @:Page ID's @pagesID
         }
    
    <div id="leaderboard" class="advert-carousel owl-carousel owl-theme" style="opacity: 1; display: block;">
        <div class="owl-wrapper-outer">
    <div class="headerSlick owl-wrapper">
        @{
        foreach(var item in selection){
            var dateRange = item.GetPropertyValue<Diplo.DateRangePicker.DateRange>("campaignDate");
            <p>Your date range is from @dateRange.StartDate to @dateRange.EndDate</p>
            var typedMultiNodeTreePicker = item.GetPropertyValue<IEnumerable<IPublishedContent>>("location");
            foreach (var item2 in typedMultiNodeTreePicker)
                {
                    adPlace = string.Concat(@item2.Id.ToString(),"  ",adPlace);
                }
    
            @:ad target ID's @adPlace
            if(item.HasValue("location") && pagesID.Contains(adPlace.ToString())){
    
    
    
    
    <script>alert("yes")</script>
    
    
    
    
            var singleLink = item.GetPropertyValue<Link>("links");
                if(singleLink != null)
                {
                    if(item.HasValue("wideImage"))
                        {  
                        IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                            <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target">
                                <img class="tag-headadvert img-responsive" style="display: inline;" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a>
                            </div> 
                        }
                }
            }
    
            else if (item.IsNull("location")){
            var singleLink = item.GetPropertyValue<Link>("links");
                if(singleLink != null)
                {
                    if(item.HasValue("wideImage"))
                        {  
                        IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                           <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target"><img class="img-responsive" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a></div> 
                        }
                }
            }
        }
    }
    

    But the important bit is this:

     if(item.HasValue("location") && pagesID.Contains(adPlace.ToString())){ 
    

    Which works if i give it one value, like 3221, but if i pick multiple values like "3221 7372" then it wont match anything.

    You can see here, https://gyazo.com/5ac3d0923a5fe48af1a9029d26e12f69 the 2 items have a matching ID in both, but nothing as a result of the match, which makes me think its not seeing a match.

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 16:06
    Kieron
    0

    Also, you may wish to notify whomever, my script tags in my code are actually executed on submit, even on this forum lol.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 14, 2018 @ 16:11
    Jan Skovgaard
    1

    Hi Kieron

    Ah ok, I misunderstood your previous follow up question.

    In that case where you have multiple id's stores that you need to compare I would say that you need to loop over all of those selected nodes and then in the loop do the .contains part - Does this make sense? :)

    /Jan

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 16:14
    Kieron
    0

    It does make sense, but me typing it out successfully is another issue :P

    Thank you, I will try and have a go but not really sure where to begin.

  • Kieron 152 posts 390 karma points
    Sep 19, 2018 @ 10:59
    Kieron
    0

    Hey Jan, ive taken some time and still not figured this out unfortunately, would you be able to provide a hint, even if its just quick pseudo code, on how to execute your suggestion. :-)

    Thanks again

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 19, 2018 @ 18:16
    Jan Skovgaard
    1

    Hi Kieron

    Sorry for my late reply - I have been meaning to get back to you all week but there has been too much on my plate to sit down and write you a proper reply, which can help you. The thing is that I need some time to setup a mini test for myself to play around with too before I guide you further.

    I hope to have some time available for this by tomorrow unless someone else beats me to it of course - Hope that's ok with you :)

    Cheers, Jan

  • Kieron 152 posts 390 karma points
    Sep 20, 2018 @ 09:16
    Kieron
    0

    Hey Jan, apologies if your busy its no trouble :-)

    I got somewhere with someone else's help elsewhere, building on your answers, but it's not quite meeting the needs I have, if I put the new code in below, you'll see it's using an array and a loop to kinda achieve the goal.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using RJP.MultiUrlPicker.Models;
    @{
    string pagesID = "";
    string adPlace = "";
    var selection = Umbraco.TypedContent(1384).Children("headerAdvert").Where(x => x.IsVisible() && x.GetPropertyValue<bool>("active"));
        foreach(var item in Model.Content.AncestorsOrSelf(1).OrderBy(x => x.Level))
            {
                pagesID = string.Concat(@item.Id.ToString(),"  ",pagesID);
            }
                pagesID = string.Concat(@Model.Content.Id.ToString(),"  ",pagesID);
         }
    
    <div id="leaderboard" class="advert-carousel owl-carousel owl-theme" style="opacity: 1; display: block;">
        <div class="owl-wrapper-outer">
    <div class="headerSlick owl-wrapper">
        @{
        foreach(var item in selection){
            var dateRange = item.GetPropertyValue<Diplo.DateRangePicker.DateRange>("campaignDate");
           @* <p>Your date range is from @dateRange.StartDate to @dateRange.EndDate</p> *@
            var typedMultiNodeTreePicker = item.GetPropertyValue<IEnumerable<IPublishedContent>>("location");
            foreach (var item2 in typedMultiNodeTreePicker)
                {
                    adPlace = string.Concat(@item2.Id.ToString(),"  ",adPlace);
                }
    
            if(item.HasValue("location")){
            var targetsToMatch = pagesID.Split(' ');
                    foreach (var it3m in targetsToMatch) {
                        if (adPlace.Contains(it3m)) {
                            var singleLink = item.GetPropertyValue<Link>("links");
                            if(singleLink != null)
                            {
                                if(item.HasValue("wideImage"))
                                    {  
                                    IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                                        <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target">
                                            <img class="tag-headadvert img-responsive" style="display: inline;" src="@mediaItem.Url" 
                                            width="100%" alt="@singleLink.Name" /></a>
                                        </div> 
                                    }
                            }
                        break;
                    }
                }
            }
    
            else if (item.IsNull("location")){
            var singleLink = item.GetPropertyValue<Link>("links");
                if(singleLink != null)
                {
                    if(item.HasValue("wideImage"))
                        {  
                        IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                           <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target"><img class="img-responsive" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a></div> 
                        }
                }
            }
        }
    }
    </div>
    </div>
    </div>
    

    But I think something is wrong because, and I don't know if your able to glean what I'm building but its an Advert system, so I'm looking at the Advert location given (adPlace) then comparing it to the Current Page ID, and all its parents, (pagesID) because I want an advert placed on "Events" to hit Events and all the children events for example. But I think I need to exclude the tippy top ID because I'm getting an advert everywhere.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 20, 2018 @ 17:28
    Jan Skovgaard
    0

    Hi Kieron

    Ok I'm afraid that I have lost track of what it is you're trying to do - But I can't see the .Contains method in your above code now?

    But looking at it makes me thing you perhaps you should consider placing some of the logic in a controller since there is quite a lot going on in the view now :) - But easy for me to say since I usually don't write controllers myself (My wonderful backend colleagues handle that part) :-)

    Ehm, but my thinking was that you should be able to loop over each of the possible add places and inside that loop do the comparison against the PagesId.

    But now I can't even remember if you MNTP is saving a comma separated string of id's or if you're getting the UDI's instead? But let's say you have the page id's available like "1023, 1233, 3002, 6789" then i would loop over all of the adPlace id's and run pagesID.Contains(adPlace.ToString()) to see if there is a match. But please note that this might be possible to do in a smarter fashion using LINQ perhaps.

    Does this make any sense and am I even on the right track in regards to what you're trying to do?

    /Jan

  • Kieron 152 posts 390 karma points
    Sep 21, 2018 @ 08:19
    Kieron
    0

    Yeah thats basically it! though ive not yet got too using Controllers, or heard of LINQ, lol.

    But yeah, user picks 'adPlace' as like a top level node, and then i want to display an advert on all the pages that match adPlace AND its children.

    ps the Contain is now roughly half way through that code.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 23, 2018 @ 17:10
    Jan Skovgaard
    0

    Hi Kieron

    What does your backoffice setup for this look like?

    But yeah, user picks 'adPlace' as like a top level node, and then i want to display an advert on all the pages that match adPlace AND its children.

    Then I'll see if I can setup an instance like you have done in order to play around with some code for you to try out.

    /Jan

  • Kieron 152 posts 390 karma points
    Oct 01, 2018 @ 13:46
    Kieron
    0

    Hey Jan, apologies for the delay, I have it set up like this:

    Document Type

    Content View

    So you'd set up an Advert, then if you see in that example the advert location is set to news, id need it on News, and all of News Children, that's why I'm doing the array of current page ID + all parents, to check if the current page or any of its parents, has an advert associated with it, and thus display it.

    Thanks :)

  • Kieron 152 posts 390 karma points
    Sep 14, 2018 @ 15:03
    Kieron
    0

    Scratch that it works, I hadn't set the ad to show where I was refreshing the page, lol.

    Thanks!

  • Kieron 152 posts 390 karma points
    Nov 07, 2018 @ 15:34
    Kieron
    0

    So I have refined everything and now I'm at this stage, albeit still not working:

    I have 2 variables: pagesID & adPlace and these might exist like

    pagesID: 1351 1348 adPlace: 1348 1067

    Then the below code runs, essentially as said above, im looking to see if any of the numbers in pagesID are in adPlace and this works if the number in pagesID is only one number, or it is first, however see my example above, adplace has 1348 and 1067, so technically there is a match as 1348 exists within pagesID but because it is not first, the code does not find a 'match'.

    var targetsToMatch = pagesID.Split(' ');
       foreach (var pagesUrl in targetsToMatch)
                    {
                        if (adPlace.Contains(pagesUrl) && hasValue == false)
                           {
                               Do X
                           }
                            hasValue = true;
                        }
    

    Thank you

  • Kevin Jump 2341 posts 14868 karma points MVP 8x c-trib
    Nov 09, 2018 @ 13:25
    Kevin Jump
    1

    Hi Kieron ,

    looking at this bit of code specifically (not all the code above that) - Umbraco has a number of extension functions to help you with this.

    first there is .ToDelimitedList() which will split the string in to its items (remove blanks) and give you lists .

    so pageIds.ToDelimitedList(" ") will give you the list of ids.

    Then .ContainsAny(list) will tell you if one list contains any values from another list -

    So if you get two lists you can see if one contains any items from the other.

    So in its simplest form this can be written as:

    if (adPlace.ToDelimitedList(" ").ContainsAny(pagesID.ToDelimitedList(" ")))
    {
         // do x
    }
    
  • Kieron 152 posts 390 karma points
    Nov 09, 2018 @ 13:37
    Kieron
    0

    This is looking good. I will admit I have got it working a different way, but it is a complete mess because I basically gave up on the idea.

    I'm going to be able to replace alot of code with that little bit lol.

    Thanks very much!

Please Sign in or register to post replies

Write your reply to:

Draft