Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Feb 05, 2015 @ 21:32
    René Andersen
    0

    AncestorOrSelf on certain level

    Hi,

    Is it possible to show all ancestors on a certain level without showing "self / current" page. I am using "AncestorOrSelf(2)" and that shows all ancestors like this:

    first-item
    second-item
    third-item

    But my problem is that I am currently on "first-item" page and I want to show related pages from here without showing the page im already on. It should look like this:

    second-item
    third-item

    And if I am on "second-page" then it should look like this:

    first-item
    third-item

    I hope that my description makes sense.

    My current code looks like this:

    @{
    if (@CurrentPage.DocumentTypeAlias == "Portfolioitem")
    {
    var section = CurrentPage.AncestorOrSelf(2);
    {
    var childItems = section.Portfolioitem;
    foreach (var page in childItems)
    {
    Do something
    }
    }
    }
    }

    // René

  • Tobias Klika 101 posts 570 karma points c-trib
    Feb 05, 2015 @ 23:11
    Tobias Klika
    2

    I am not that into the dynamic thing,
    here's a solution for writing this in a typed manner:

    Umbraco.TypedContent((int)CurrentPage.Id).AncestorOrSelf(2).Children.Where(x => x.Id != CurrentPage.Id)
    
  • John Churchley 272 posts 1258 karma points c-trib
    Feb 06, 2015 @ 00:15
    John Churchley
    1

    Alternative

    Model.Content.Siblings().Where(x => x.Id != Model.Content.Id)
  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 11:26
    René Andersen
    0

    Thanks guys now I am on the right track but I still have a problem.

    At the moment I can only get data such as "@item.name, @Item.Url" but how do I get my own data like "title, text, teaser"?

    My code looks like this now:

    @if (@CurrentPage.DocumentTypeAlias == "Portfolioitem")
    {
    var siblings = Model.Content.Siblings();
    foreach (var item in siblings.Where(x => x.Id != Model.Content.Id))
    {
    <a title="@item.Name" href="@item.Url">@item.Name</a>
    }
    }

    If I try to insert CurrentPage instead of Model then it fails so I dont know what I am missing.

    // René

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 06, 2015 @ 12:02
    Dennis Aaen
    101

    Hi René

    With this code you should be able to get title and teaser, the thing is that you are strongly typed Razor so you need to use .GetPropertyValue("") to get the value from a field.

    @if(CurrentPage.DocumentTypeAlias=="Portfolioitem"){
     
    var siblings = Model.Content.Siblings();
     
    foreach(var item in siblings.Where(x => x.Id != Model.Content.Id))
     
    {
       
    <a title="@item.GetPropertyValue('title')" href="@item.Url">@item.GetPropertyValue("teaser")a>
     
    }
    }

    Somtimes is necessary to specify the retun value, this can be done by

    @item.GetPropertyValue<string>("teaser")

    Hope this helps,

    /Dennis

  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 12:56
    René Andersen
    0

    Hi Dennis

    Thank you, that solved it. :-)

    What do you mean about strongly typed Razor? Is there a better solution? I am still pretty new to programming and it would be nice to learn it the right way the first time.

    // René

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 06, 2015 @ 13:17
    Dennis Aaen
    0

    Hi René,

    It's because that there are two implementations of Razor in Umbraco, one is the dynamics CurrentPage, and the other is strongly typed Model.Content. The different is that the dynamics implemtation is more concise and with the strongly typed razor you will get intellisense if you are using e.g visual studio or webmatrix.

    You could try to see Jeavon´s slides from the umbOktoberFest 2014 which you can download here.

    /Dennis

  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 13:26
    René Andersen
    0

    Hi Dennis,

    Thanks again I really appreciate it.

    // René

  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 14:34
    René Andersen
    0

    Hi again,

    I have one more question regarding this.

    This code just works perfect:

    @if(CurrentPage.DocumentTypeAlias == "Portfolioitem")
    {

    var siblings =Model.Content.Siblings();
     
    foreach (var item in siblings.Where(x => x.Id!=Model.Content.Id))
     
    {
       
    <a title="@item.GetPropertyValue('title')" href="@item.Url">@item.GetPropertyValue("teaser")</a>
     
    }
    }

    But how is it possible to implement a function that only show the same category as the current page?

    At the moment the current page is in the category "graphics" and therefor I need it to only show items from that category.

    // René

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 06, 2015 @ 14:48
    Dennis Aaen
    0

    Hi René

    Don´t know if is possible for you to do this like you do the check if so you don´t get currentpage in your navigation.

    Perhaps you could, do something like this, but the code blow is untesed.

    foreach(var item in siblings.Where(x => x.DocumentTypeAias == Model.Content.DocumentTypeAias).Where(x => x.Id != Model.Content.Id))

    And if you have a property on your document type so you can hide item from the navigation then I would implement this where too. The alias of the property needs to be umbracoNaviHide and the type is true/false

    .Where(x => x.IsVisible())

    Hope this helps,

    /Dennis

  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 15:08
    René Andersen
    0

    Hi Dennis,

    The category is set with a radio button does that change anything regarding your code above?

    // René

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 06, 2015 @ 15:27
    Dennis Aaen
    0

    Hi René

    Here is the documentation on how you can get the value from radio button list https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/RadioButton-List

    Perhaps you can do something like this:

    var preValue = Umbraco.GetPreValueAsString(Model.Content.GetPropertyValue<int>("category"));

    foreach(var item in siblings.Where(preValue).Where(x => x.Id!=Model.Content.Id))

    The code above is just a guess on how you maybe can do it. So it should take all siblings where the radio button is set to the same as the current page.

    Hope this helps, if not I am out for ideas of how you could solve this at the moment.

    /Dennis

  • René Andersen 238 posts 684 karma points
    Feb 06, 2015 @ 16:03
    René Andersen
    0

    Thanks Dennis I will give it a try and post the result in here.

    Have a nice weekend.

    // René

  • René Andersen 238 posts 684 karma points
    Feb 08, 2015 @ 19:40
    René Andersen
    0

    I have tried several solutions but none of them works. But I think that Dennis is on the right track.

    Does anyone know a different approach?

    At the moment it works so that it shows all items from all categories without showing the current item.

    What I basically need is:

    Do NOT show current item but show all other items in same category as current item which is set by a radio button.

    I have attached this again because the post now is on two pages :-)

    And my code looks like this:

    @if (@CurrentPage.DocumentTypeAlias == "Portfolioitem")
    {
    var siblings = Model.Content.Siblings();
    foreach (var item in siblings.Where(x => x.Id != Model.Content.Id))
    {
    <a title="@item.Name" href="@item.Url">@item.Name</a>
    }
    }

    Thanks in advance!

    // René

  • René Andersen 238 posts 684 karma points
    Feb 10, 2015 @ 12:26
    René Andersen
    0

    I have still not managed to get it working, please look at the post above.

    Does anyone know how to achieve this?

    Thanks!

    // René

Please Sign in or register to post replies

Write your reply to:

Draft