Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 936 posts 2571 karma points
    Mar 02, 2018 @ 08:16
    Claushingebjerg
    0

    How to get the currentpage from a nested partial

    I have a partial, where i load another partial, which is a nav. (Some stacked content thingie).

    But pullin @Model.Content gets me the stacked content item, not the current page.. @CurrentPage does the same...

    How do i get the actual page i'm on?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 02, 2018 @ 08:44
    Michaël Vanbrabandt
    0

    Hi Claushingebjerg,

    you could make your own ViewModel where you pass the Page as a property to the Partial where you need it.

    Hope this helps

    /Michaël

  • Claushingebjerg 936 posts 2571 karma points
    Mar 02, 2018 @ 08:46
    Claushingebjerg
    0

    In theory, yes, but a snippet or somehting would be nice as i have no idea how...

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 02, 2018 @ 08:52
    Michaël Vanbrabandt
    1

    Claushingebjerg,

    No problem:

    public Class PartialViewModel {
        public IPublishedContent CurrPage { get; set; }
    }
    

    Then where you load the nav partial you pass in this class object:

    @Html.Partial("PartialOfNavPath", new PartialViewModel() { CurrPage = Model.Content })
    

    *Untested code, so don't shoot me when I have a typo.

    Hope this helps

    /Michaël

  • Claushingebjerg 936 posts 2571 karma points
    Mar 02, 2018 @ 08:49
    Claushingebjerg
    0

    And i don't do backend code, so no visual studio.

    Please dont tell med this requires VS and compiling and stuff. I was hoping this would be relatively simple.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 02, 2018 @ 08:53
    Michaël Vanbrabandt
    0

    Ah ok didn't see this post that you don't want to use VS.

  • Claushingebjerg 936 posts 2571 karma points
    Mar 02, 2018 @ 08:56
    Claushingebjerg
    0

    Yeah, i've built umbraco sites since 2008 without it :), so no need to start now... i hope.

    But lately i've been running into more and more relatively simple stuff, that seems to require a VS solution :(...

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Mar 02, 2018 @ 09:43
    Steve Morgan
    100

    Hi,

    Purests will not like this but it will work!

    Use the TempData!

    In your view set the value then it will be available in all partials:

    @{
        Layout = "Master.cshtml";
    
        ViewData["umbracoPageId"] = Model.Content.Id;
    }
    
    @Html.Partial("TestPartial")
    

    Then "get" it in your partial and do what you want with it (UmbracoHelper!) proof of concept below!

    @{ 
        var currentPageId = ViewData["umbracoPageId"];
    
        var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
        var currentPage = umbracoHelper.TypedContent(currentPageId);
    }
    <h1>this is a test parial - pageID: @currentPageId</h1>
    <h2>@currentPage.Name</h2>
    

    HTH (disclaimer I used VS to write it ;) )

  • Claushingebjerg 936 posts 2571 karma points
    Mar 02, 2018 @ 10:28
    Claushingebjerg
    0

    Thanks - got an error , but rewrote it to:

     @{
            var currentPageId = ViewData["umbracoPageId"];
            var currentPage = Umbraco.TypedContent(currentPageId);
        }
    
        <p>@currentPage.Name</p>   
    

    Which works fine. THANKS

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Mar 02, 2018 @ 10:50
    Steve Morgan
    0

    Of course you can just do that - how stupid of me! Glad it put in you in the direction of a solution though!

Please Sign in or register to post replies

Write your reply to:

Draft