Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 967 karma points c-trib
    Aug 20, 2023 @ 22:47
    Murray Roke
    0

    Access ModelsBuilder properties in a view using a PublishedContentWrapped

    Hi, I have a hijacking controller similarly to documented here: https://docs.umbraco.com/umbraco-cms/reference/routing/custom-controllers

    Aside: I am using PublishedContentWrapped because the additional properties I'm adding to the viewModel should only have request scope, but if I extend the models builder object directly then that object has a longer scope and could be accessed by another user.

    The documentation says I can access modelsBuilder properties like so:

    // or using Modelsbuilder
    @Model.ProductDescription
    

    However ProductDescription is not a property on public class MyProductViewModel : PublishedContentWrapped

    My current workaround is to add a property to MyProductViewModel like so:

    public ProductPage Page => (ProductPage)this.Unwrap() ;

    Then in my view do this:

    // or using Modelsbuilder
    @Model.Page.ProductDescription
    

    Is the documentation incorrect? or am I misreading it?

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Aug 21, 2023 @ 07:33
    Huw Reddick
    0

    do you have this at the top of your view?

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyProductViewModel>
    

    you can always access the values from your page node using the value method

    Model.Value<int>("intPageSize")
    
  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Aug 21, 2023 @ 07:41
    Huw Reddick
    1

    To be able to use Modelsbuilder then your custom view should inherit from your Modelsbuilder Page model and not from publishcontentwrapped

    public class MyProductViewModel : MyUmbracoPage
    {
        ....
    }
    
  • Murray Roke 503 posts 967 karma points c-trib
    Aug 21, 2023 @ 20:45
    Murray Roke
    100

    Hi Huw,
    TLDR: I was misreading the docs, use PublishedContentWrapped if you're NOT using models builder.

    DETAIL: I should have Inherited from the models builder object, and created my inherited object on each request from within my controller using the constructor like so:

    var viewModel = new MyProductViewModel(CurrentPage, new 
    PublishedValueFallback(_serviceContext, _variationContextAccessor))
        {
            CustomerInfo = GetCustomerInfo()
        };
    

    I was confused as to when would one use PublishedContentWrapped but after re-reading the docs, I think that is intended to be used if you're NOT using models builder.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies