Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 118 posts 362 karma points c-trib
    May 09, 2021 @ 11:26
    Martin Rud
    0

    How to get the "original" Model when in partial view with it "own" Model?

    Hi,

    I call a view from a template and pass an 'item' obejct to it:

    @Html.Partial("myView"), item)
    

    It works fine and I can call properties in my 'item' object in the view with

    @Model.Value("propName")
    

    But how can I get the Model of the page (node) that the view is called from? Model no longer refers to the page that the website visitor is viewing.

  • Marc Goodson 1529 posts 10230 karma points MVP 6x c-trib
    May 09, 2021 @ 11:57
    Marc Goodson
    100

    Hi Martin

    If you make your partial view have an inherits statement at the top eg:

    @inherits UmbracoViewPage<YourCustomModel>
    

    (instead of any normal MVC @model declaration you might have)

    Then you can access the current page model via:

    Umbraco.AssignedContentItem
    

    eg @Umbraco.AssignedContentItem.Name

    regards

    Marc

  • Martin Rud 118 posts 362 karma points c-trib
    May 09, 2021 @ 12:13
    Martin Rud
    0

    Thanks, Marc! That was just my need! :)

    Had

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedElement>
    

    Changed it to:

    @inherits UmbracoViewPage<IPublishedElement>
    

    And now I can call for instance:

    Umbraco.AssignedContentItem.Children().Where(x => x.IsVisible());
    
Please Sign in or register to post replies

Write your reply to:

Draft