Copied to clipboard

Flag this post as spam?

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


  • SandyK-W 23 posts 63 karma points
    Aug 30, 2024 @ 11:31
    SandyK-W
    0

    Partial view issues v13

    Hi,

    I'm having issues with our footer partial which is called from the master/layout template used on all pages.

    I've tried a variety of approaches but the model is always null meaning I can't access things like the navigation (umbraco content pickers) and the copyright text (plain text field)

    Here you can see that I've tried using a fallback then checked if the model even has anything in it (spoilers, it's null)

    @inherits UmbracoViewPage<Footer>;
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    <div class="footer-bottom">
         @if (Model != null && !string.IsNullOrEmpty(Model.CopyrightText))
         {
             <p>Copyright &copy; @DateTime.Now.Year @Model.CopyrightText</p>
         }else{
             <p>Copyright &copy; @DateTime.Now.Year CompanyName</p>
         }
    
         @if (Model != null)
         {
             <p>Model is not null</p>
             <p>CopyrightText: @Model.CopyrightText ?? "No value or null"</p>
         }
         else
         {
             <p>Model is null</p>
         }
     </div>
    

    Any pointers are welcome, I'm on the latest v13 and using Models Builder. the above is a snippet that shows the inheritance and the bottom of the file where the model is being called

  • Diljith Peter 9 posts 80 karma points
    Sep 02, 2024 @ 05:40
    Diljith Peter
    0

    Pass Model rather than Model.Footer from Master layout to Footer Partial and fetch values from root node directly. Use code like below for that,

    var home = (Home)Model.Root();

    @home.CopyrightText

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 08:38
    SandyK-W
    1

    Hi Diljith Peter,

    Thanks for replying, I don't think this would work as the footer navigation is stored in a tree node called footer under a different root node called "Global" so "Global > Footer" but the site pages are under the "Home" root node

    So it's my layout/master template calling the partial (this will be true for the whole site, not my personal choice but it is how the site was set up by the third party who did the build for version 8)

    Hopefully this information is helpful!

    Thanks, Sandy

  • Joppe Ruessink 18 posts 168 karma points
    Sep 02, 2024 @ 09:50
    Joppe Ruessink
    0

    Hi Sandy,

    Like I explained to you in this thread: https://our.umbraco.com/forum/using-umbraco-and-getting-started/114614-umbraco-v13-content-node-as-a-class-for-use-in-all-views-and-partials

    You could add the the "Footer" content to the ViewBag via the same method as explained before.

    Of course it is not the best practice to add all your content to the ViewBag however because you have already implemented this you could extend it and make use of it one more time.

    Greetings, Joppe

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 10:21
    SandyK-W
    0

    Hi Joppe,

    That's fair! It was only later I realised the parallel to the other question. the thing is shouldn't the partial be able to access the content in its doctype without the viewbag?

    Maybe I need to look at a different approach entirely for this content as the footer will appear on all pages. It still feels messier than it should, is there maybe a best practice for this organising this kind of structure?

    the only choice driving this separation is purely how it was done on the older version of the site

    Thanks, Sandy

  • Joppe Ruessink 18 posts 168 karma points
    Sep 02, 2024 @ 11:20
    Joppe Ruessink
    100

    Hi Sandy,

    I am a back-end developer (so not that used with the .cshtml side) using Umbraco but if i am not mistaken this is the reason why your @Model is null:

    What you are saying is correct: "the thing is shouldn't the partial be able to access the content in its doctype without the viewbag". However this is only the case when you develop a template (.cshtml page) for a specific doctype, not all doctypes have templates.

    If you develop a view for a doctype (for example Homepage, so the Homepage.cshtml view) then you can read the values ​​of the content using @Model and display them in the view. However, the Footer doctype is not a page with its own template (there is no page: "https://myumbracosite.com/en/footer") so you cannot read it using @Model. Generic settings such as main menu, footer and site settings cannot (as far as I know) be "injected" into the @Modal property.

    In summary: if you are not developing a content page/view for a doctype (for example the Footer partial) it is separate from @Model and you need to retrieve the data in a different way.

    Please correct me if i am wrong! I am very interested in other solutions.

    Greetings, Joppe

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 14:01
    SandyK-W
    0

    Hi Joppe,

    That makes sense I think. I'll try a few things then come back here

    Thanks, Sandy

  • Simon Napper 115 posts 349 karma points
    Sep 02, 2024 @ 13:56
    Simon Napper
    0

    Hi Sandy,

    So when you call your partial, are you passing a populated Footer Model to it?

    So guessing you have something like:

    @await Html.PartialAsync("Footer")

    But you probably need something like:

    @await Html.PartialAsync("Footer", myPopulatedFooterObject)

    Which, depending on your architecture, you can populate when the page loads via a RenderController or you might need a ViewComponent rather than a partial. If you're not passing that object, then it'll always be null as nothing is going to it (unless I've misunderstood how you have this working!).

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 14:05
    SandyK-W
    0

    Hi Simon,

    I'm having trouble working out how to correctly pass the model to the partial.

    If I use the models builder model definition in the partial itself it falls over as it's then got two models competing on the page.

    Passing it through though is proving quite hard. I'm converting a version 8 site from .NET so I'm getting tripped up a lot trying to work through it as it's quite different functionally.

    If I was to swap the viewbag to the "Global" root node could I use that to access it's children on the page? Or would it be better to add footer and header as their own functions?

    We used to have:

    @await Html.PartialAsync("layout/Footer", globalContent.DescendantOfType("footer"));
    

    But now I am struggling to replace this as accessing the global root seems to be harder than I thought.

    Thanks, Sandy

  • Simon Napper 115 posts 349 karma points
    Sep 02, 2024 @ 14:07
    Simon Napper
    0

    Hi Sandy,

    So in the old site, how was globalContent defined and populated?

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 14:32
    SandyK-W
    0

    Hi Simon,

    Site defined globalContent as:

    var globalContent = Umbraco.ContentAtRoot().First(c => c.ContentType.Alias == "global");
    

    If I use this now I get:

    ObjectDisposedException: Cannot access a disposed object. Object name: 'snapshot'.

    I fear I am realising how little I actually know about Umbraco XD

  • Simon Napper 115 posts 349 karma points
    Sep 02, 2024 @ 15:29
    Simon Napper
    0

    Hi Sandy,

    Sounds like an annoying error!

    So is this

    var globalContent = Umbraco.ContentAtRoot().First(c => c.ContentType.Alias == "global"); 
    

    in the view that is calling the partial or is it happening in a service/controller?

  • SandyK-W 23 posts 63 karma points
    Sep 02, 2024 @ 15:42
    SandyK-W
    0

    Hi Simon,

    At the moment it is in the View, though I may just have my logic twisted.

    I was using it to get the global root node to then use descendant of type for the footer partial.

    I almost wonder if I should just bin the files and start over fresh just so I can get rid of the umbraco 8 stuff

Please Sign in or register to post replies

Write your reply to:

Draft