Copied to clipboard

Flag this post as spam?

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


  • Mads Sørensen 188 posts 433 karma points
    Mar 09, 2016 @ 10:28
    Mads Sørensen
    0

    MNTP - Check string for CurrentPage Id

    Hi Guys

    I Got theses two areas at my website:

    Employees:

    • Mads
    • Kristian
    • Sophie

    Areas:

    • Development
    • Design
    • Tech

    The employees can be attached to multiple areas with an property of Multinode Treepicker.

    When i'm at the page development i would like to show alle employees that have the value og e.g. development (Mads and Sophie).

    And my code looks like this right now:

    @foreach(var item in CurrentPage.AncestorOrSelf(2).Descendants("Ansatte").Where("Visible")){
    
       @*Check if item.Areas Contains CurrentPage.Id*@
       if(item.areas.Contains(CurrentPage.Id)){
          <h1>@item.Name etc.</h1>
       }
    }
    
  • Mads Sørensen 188 posts 433 karma points
    Mar 09, 2016 @ 10:33
    Mads Sørensen
    0

    Right now if i output the MNTP It just gives me an list of ID's of the areas that i picked.

    @item.areas 
    

    1012, 1013

    Is it somehow possible to check for the sellected ids?

    Something like:

    if(Item.areas.Contains(CurrentPage.Id)){
    
       //then show stuff
    
    } 
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 09, 2016 @ 13:34
    Dave Woestenborghs
    0

    Splitting the string will be safer. Because when check for contains on the string with the value "10" it will match also on this string "210,342"

    Something like this :

    string areas = item.areas;
    
    var areaIdArray =  areas.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries);
    
    areaIdArray.Contains(CurrentPage.Id.ToString());
    

    Dave

  • Mads Sørensen 188 posts 433 karma points
    Mar 09, 2016 @ 15:02
    Mads Sørensen
    0

    Hi Dave

    I've tried your suggestion but i get an error like this:

    CS1973 : ' string [ ] ' is not a useful method named ' Contains '

    My code looks like this:

    @foreach(var item in CurrentPage.AncestorOrSelf(2).Descendants("Ansatte")){
       string areas = item.fag;
    
       var areaIdArray =  areas.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries);
    
      areaIdArray.Contains(CurrentPage.Id.ToString());
    <h1>@item.name</h1>
    }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 09, 2016 @ 15:04
    Dave Woestenborghs
    0

    Hi Mads,

    Just typed this from memory so it can contain errors :-)

    Maybe you can try this :

     var areaIdArray =  areas.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries).ToList();
    

    Dave

  • Mads Sørensen 188 posts 433 karma points
    Mar 09, 2016 @ 15:13
    Mads Sørensen
    0

    ...well memorized anyways :)

    It dosent give me an error now but I'm not sure how to use it as a check? I've tried to use it in an if statement but no positive result:

    @foreach(var item in CurrentPage.AncestorOrSelf(2).Descendants("Ansatte")){
    
       string areas = item.fag;
       var areaIdArray =  areas.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries).ToString();
    
       if(areaIdArray.Contains(CurrentPage.Id.ToString())){
          <p>Show me at this page</p>
       }else{
          <p>Do not show</p>
         }
     }
    

    And now it just output "Do not show" :)

  • Mads Sørensen 188 posts 433 karma points
    Mar 10, 2016 @ 08:19
    Mads Sørensen
    0

    Hi again

    After a good nights sleep i came out with this solution:

    @foreach(var item in CurrentPage.AncestorOrSelf(2).Descendants("Ansatte")){
    
              var fag = item.Fag.ToString();
    
              if(fag.Contains(CurrentPage.Id.ToString())){
                <p>Show me at this page</p>
              }else{
              <p>Do not show</p>
              }
    }
    

    I've no idea if this is the right way to solve it?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 10, 2016 @ 08:21
    Dave Woestenborghs
    100

    Hi Mads

    You do a ToString after the string split. that should be a tolist

    So this should work

    @foreach(var item in CurrentPage.AncestorOrSelf(2).Descendants("Ansatte")){
    
       string areas = item.fag;
       var areaIdArray =  areas.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries).ToList();
    
       if(areaIdArray.Contains(CurrentPage.Id.ToString())){
          <p>Show me at this page</p>
       }else{
          <p>Do not show</p>
         }
     }
    

    Dave

  • Mads Sørensen 188 posts 433 karma points
    Mar 10, 2016 @ 08:27
    Mads Sørensen
    0

    Hi Dave Well i forgot to mention that my previous post worked, but i was not sure if it was the proper way to do it? Maybe some weak points? :D

    But your solution works perfect to - so i go with that solution - thanks for your help I was really stuck on this one :)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 10, 2016 @ 08:29
    Dave Woestenborghs
    0

    It's not entirerly fail safe

    Splitting the string will be safer. Because when check for contains on the string with the value "10" it will match also on this string "210,342"

  • Mads Sørensen 188 posts 433 karma points
    Mar 10, 2016 @ 08:45
    Mads Sørensen
    0

    But i guess sense the page id i unique it should not be a problem?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 10, 2016 @ 08:51
    Dave Woestenborghs
    0

    yes page id is unique...But the check matches also non unique id's.

    For example your string contains following ids : 1111,2101,4237

    The page id of the current item is 101...

    If you do a contains it will match because of 2101.

    Dave

  • Mads Sørensen 188 posts 433 karma points
    Mar 10, 2016 @ 08:55
    Mads Sørensen
    0

    Ahhh...true...did not see that one :)

    Thanks again Dave for your reply :D

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies