Copied to clipboard

Flag this post as spam?

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


  • Paul Seal 405 posts 2214 karma points MVP 3x c-trib
    Jul 23, 2015 @ 08:04
    Paul Seal
    0

    Accessing CurrentPage properties in a partial which is using a custom model

    Hi all I thought I would share this with you as when I was looking for something like this a while ago, I would have appreciated finding it.

    When I have a partial which is declared like this:

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

    I am unable to use @CurrentPage to get any properties from the page.

    Normally I would make the model inherit from the render model like this:

    using Umbraco.Web;
    
    namespace MySite.Web.Models
    {
        public class ContactModel: Umbraco.Web.Models.RenderModel
        {
    
            public ContactModel()
                : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
            {
            }
        }
    }
    

    The above would then allow me to use @Model.Content and I'd be able to access the properties that way.

    The model I was using this time didn't allow for that. I didn't like the empty constructor.

    I found a way of accessing the page's properties without needing to inherit from the render model.

    The way I found was to add this to the top of the page:

    @{
        var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
    }
    

    I can now use @thisPage to access the page's properties the same as I would have done with @CurrentPage

    I'm sure someone is going to come on here and tell us there is a much better way, but I see that as a good thing. At least we are discussing it.

    I hope this post helps someone, if it does, please let me know.

    Cheers

    Paul

  • Alex Skrypnyk 5741 posts 21797 karma points MVP 4x admin c-trib
    Jul 23, 2015 @ 11:41
    Alex Skrypnyk
    1

    Hi Paul,

    Did you try to use just:

    Umbraco.AssignedContentItem

    This is you actual current page, if you want strongly typed you have to use Model builders or something like that.

    If you use this one

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Try to access to data via @Model, without .Content

    Thanks, Alex

  • Paul Seal 405 posts 2214 karma points MVP 3x c-trib
    Jul 23, 2015 @ 12:00
    Paul Seal
    0

    Hi Alex I did try to use just Umbraco.AssignedContentItem but it didn't allow me to access the properties I had created for the page.

    Thats why this one worked better for me:

    var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
    

    With regards to:

    If you use this one

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Try to access to data via @Model, without .Content

    That doesn't work for me as it is getting the data from my Model, I want to get at the data from the content item.

    Thanks for replying though.

    Paul

  • Alex Skrypnyk 5741 posts 21797 karma points MVP 4x admin c-trib
    Jul 23, 2015 @ 12:26
    Alex Skrypnyk
    1

    Paul,

    But this is - var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);

    One more call to db фтв Umbraco.Content is dynamic, you have all data in the Umbraco.AssignedContentItem, just use

    Umbraco.AssignedContentItem.GetPropertyValue

    Thanks, Alex

  • Alex Skrypnyk 5741 posts 21797 karma points MVP 4x admin c-trib
    Jul 23, 2015 @ 12:27
    Alex Skrypnyk
    0

    If you need strongly typed Model use -

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder

  • Paul Seal 405 posts 2214 karma points MVP 3x c-trib
    Jul 23, 2015 @ 13:05
    Paul Seal
    0

    I prefer to do it that way, but thanks for sharing yours.

    Paul

  • Liam Dilley 64 posts 190 karma points
    Jun 03, 2020 @ 06:56
    Liam Dilley
    0

    Hi Paul, Bit of a fan of yourself as you seem to be one of just a few in the Umbraco sharing your findings with everything especially in regard to Umbraco 8 changes.

    Thanks

    I been working on custom forms where I have a RenderAction to a surface controller which rendered the partial view for the form.

    I needed to fetch current page data - like the associated email so depending on what "Store" you had the contact form on it would mail the right person the submitted data.

    I too ran into this issue and was a bit lost about how to access the current page Model while already having the Model data for the form itself (Which also was built from watching one of your videos)

    var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
    

    This got me what I needed.

  • Anthony Southworth 23 posts 66 karma points
    Jun 08, 2020 @ 11:26
    Anthony Southworth
    0

    I've just been playing around with this and the best solution that I can see is a combination of the above posts:

    @inherits UmbracoViewPage<ContactModel>
    
    @{
        ContactPage page = (ContactPage)Umbraco.AssignedContentItem;
    }
    
    <h1>@page.PageProperty</h1>
    

    This seems to be a much better option than inheriting from RenderModel.

    Cheers, Tony

Please Sign in or register to post replies

Write your reply to:

Draft