Copied to clipboard

Flag this post as spam?

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


  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 12:27
    Damion
    0

    umbraco 8 - get current page from partial view

    Im following Paul Seals videos on taking a static bootstrap site and converting it to an Umbraco CMS site. The tutorial is being done on v7 but I have v8 installed.

    It's all going fine, but I can't figure out how to get the current page in a partial view.

    The tutorial uses

    @inherits UmbracoTemplatePage
    
    <h2>@CurrentPage.intro</h2>
    

    intro being a document type of text box input which I added to my home page under compositions.

    Under content I can now enter text into my new control, but Im not sure how to insert this into the <h2> tag as above.

    If needed here is the video, the relevant part is at 10:56 https://www.youtube.com/watch?v=zRDHKisHCQ&list=PL90LHquhD--OWQNLyO1-KRxVDd0NPBfZ&index=5

    I have tried using

    @inherits UmbracoViewPage
    <h2>@Model.Value("intro")</h2>
    

    but get a null reference exception.

    thanks

  • Ronak Panchal 71 posts 230 karma points
    Mar 27, 2019 @ 13:25
    Ronak Panchal
    0

    Hello Damion,

    If you want to use current pages properties in partial view then simply write as below.

     @{
        string Intro = Model.Content.GetPropertyValue<string >("intro");
      }
        <h2>@Intro</h2>
    

    Hope this will work,

    Ronak.

  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 13:33
    Damion
    0

    Hi

    thanks for your reply but Model does not seem to contain a definition for Content, any ideas?

    thanks

  • Ronak Panchal 71 posts 230 karma points
    Mar 27, 2019 @ 13:42
    Ronak Panchal
    0

    Change That @inherits UmbracoViewPage to

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage in partial view.

  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 13:43
    Damion
    0

    TemplatePage is not available in v8 as far as I'm aware, it has to be ViewPage. Interestingly, if I put a breakpoint on that line it appears that Model is null.

  • Ronak Panchal 71 posts 230 karma points
    Mar 27, 2019 @ 13:51
    Ronak Panchal
    0
    @inherits UmbracoViewPage<Items>
    

    try something like that Where Items is the model and you have to pass it.

  • Mikkel 14 posts 115 karma points
    Mar 27, 2019 @ 14:14
    Mikkel
    0

    Have you tried changing:

    @inherits UmbracoViewPage
    

    to

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    I've very similar code on my site, and the only difference seems to be this, as far as I can tell.

  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 14:21
    Damion
    0

    Hi

    Had tried that, althought the Umbraco.Web.Mvc. part of the name is unnecessary, either way the model is still null.

  • Mikkel 14 posts 115 karma points
    Mar 27, 2019 @ 14:30
    Mikkel
    0

    Hey,

    > Umbraco.Web.Mvc. part of the name is unnecessary

    Today I learned!

    Could you post some more of your code from both your partial and where you use it?

    The code you have in your original post shouldn't be the problem at least.

    In my experience the null reference exception can get thrown in instances where you use @ in front of code, already inside of a code section like

    @{ 
        @var variable = 1;
        @Model.Value("intro")
        <p>html is html</p>
    }
    

    Hope this helps, or someone more experienced can enlighten us :D

  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 14:32
    Damion
    1

    Hi all,

    the answer to this for v8 is

    @Umbraco.AssignedContentItem.GetProperty("intro").GetValue()

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Mar 27, 2019 @ 15:12
    Paul Seal
    100

    Hi Damion

    I'm glad to hear you're watching my videos :^)

    The right way to do it in v8, without ModelsBuilder, is like this:

    @inherits UmbracoViewPage
    
    <h2>@Model.Value<string>("intro")</h2>
    
  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 15:23
    Damion
    0

    Hi Paul,

    Yeah making my way through the series just now... I had actually tried that and couldn't get it to work which is why I went with what I did, probably something in my set-up. cheers

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Mar 27, 2019 @ 15:23
    Paul Seal
    1

    I suppose it depends if you are passing in a model or not from the view. If you are not passing in a specific model, then it will pass in the IPublishedContent for you automatically, meaning we can use my example above.

    Have a look at my v8 starter kit to see how I've done it on there.

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8

    Here is a partial example:

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8/blob/master/src/CSUSK.Web/Views/Partials/Content/_ArticleContent.cshtml

    It gets called from this view

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8/blob/master/src/CSUSK.Web/Views/Article.cshtml

  • Damion 96 posts 331 karma points
    Mar 27, 2019 @ 15:26
    Damion
    0

    When not passing a model and using your code above I get

    "Cannot convert method group 'Value' to non-delegate type 'object'" but I don't see why I'd get that.

    I'll have a look at those links later then,

    cheers again

Please Sign in or register to post replies

Write your reply to:

Draft