Copied to clipboard

Flag this post as spam?

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


  • Kieron 45 posts 149 karma points
    1 week ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    1 week ago
    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 45 posts 149 karma points
    1 week ago
    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 45 posts 149 karma points
    1 week ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    1 week ago
    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 45 posts 149 karma points
    1 week ago
    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 45 posts 149 karma points
    1 week ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    1 week ago
    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 45 posts 149 karma points
    1 week ago
    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 45 posts 149 karma points
    6 days ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    6 days ago
    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 45 posts 149 karma points
    6 days ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    5 days ago
    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 45 posts 149 karma points
    5 days ago
    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 11083 posts 22554 karma points MVP 4x admin c-trib
    2 days ago
    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 45 posts 149 karma points
    1 week ago
    Kieron
    0

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

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft