Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    Nested Content in separate partials

    Hi. I'm trying to separate rendering of nested content into individual partials for easy of maintanence. Using the tried method from 7:

       var sections = Model.Content.Value<IEnumerable<IPublishedElement>>("sections");
            @foreach (var item in sections)
                {
                    @Html.Partial("~/views/partials/" + item.ContentType.Alias + ".cshtml", item)
                }
    

    Throws an error: enter image description here Any idea how to fix?

  • Magnus Eriksson 70 posts 120 karma points
    1 week ago
    Magnus Eriksson
    0

    With just a tiny update for v8 I've been using this approach: https://gist.github.com/ChesterCampbellAM/2e9e14de81a4ee242eee46ce6c4936e4

    Regards, Magnus

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    Thanks Magnus

    Unfortunately im a front ender and don't use VS, so... It used to be real easy to do this - does one really have to be a backend dev to do this in 8? That would be a great step backwards. :|

  • Magnus Eriksson 70 posts 120 karma points
    1 week ago
    Magnus Eriksson
    1

    Try switching IPublishedContent to IPublishedElement

    Regards, Magnus

  • Sören Deger 718 posts 2765 karma points c-trib
    1 week ago
    Sören Deger
    0

    Hi Claus, maybe you have the wrong model? Which model do you use in your partial views?

    Best, Sören

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    My template:

    enter image description here

    My nested content partial(s):

    enter image description here

  • Magnus Eriksson 70 posts 120 karma points
    1 week ago
    Magnus Eriksson
    0

    I think Nested Content has changed from IPublishedContent to IPublishedElement, try update that?

    Regards, Magnus

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    1

    I have. It was just a typo in the first post :)

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    To clear things up a bit. I can load the partial just fine, when i'm not parsing down the ",item"

    so this works:

    @Html.Partial("~/views/partials/" + item.ContentType.Alias + ".cshtml")
    

    This doesnt

    @Html.Partial("~/views/partials/" + item.ContentType.Alias + ".cshtml", item)
    

    But i need "item" to access data, right...

  • Nik 1044 posts 4220 karma points MVP c-trib
    1 week ago
    Nik
    100

    Hey Claus,

    In your partial screen shot it shows the top of it looking as follows:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    Because you aren't specifying a model type here, the default behaviour that the UmbracoViewPage does is expect an IPublishedContent

    What you need to do is tweak this inherits statement. You have a few options for this though.

    The first, and possibly the most simple is this (which is what I think others have been encouraging you to do)

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

    In theory (I've not tested this so apologies) it should solve your issue with the binding behaviour.

    Secondly, if you wanted to make things a little more robust (granted it requires more work each time you add a new element type to your nested content), you could add a switch statement into your foreach loop as well. This way you could take advantage of ModelsBuilder to get more specific typed models in your partial views.

    I won't go into detail on this further unless you want it so give the first option a go and let us know if it works, if it doesn't I'll try and guide you through the ModelsBuilder approach :-) It should all be do-able from the Umbraco back office :-) and I'm sure you are more than capable of doing it :-)

    Nik

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    Hey Nik

    Thanks, the error went away, but i cant seem to get the value from the nested content though.

    Example: I have a nested content with the doc type "tekstspalte" on which i have a property with the alias "spalte"

    @Model.Value("spalte")
    

    returns nothing

  • Nik 1044 posts 4220 karma points MVP c-trib
    1 week ago
    Nik
    0

    That's interesting. Can you share a screen shot of what the document type "tekstspalte" looks like in the Settings section, with the property "spalte" in view?

    I've done this a few times recently and based on your description I can't see why you aren't getting anything back.

    Nik

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    enter image description here

    enter image description here

  • Nik 1044 posts 4220 karma points MVP c-trib
    1 week ago
    Nik
    0

    Hmm, that does look right. I'll see if I can find some time to do a little bit of testing for you.

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    0

    That would be awesome - and will result in a Beer on me at Codegarden ;)

  • Claushingebjerg 826 posts 2129 karma points
    1 week ago
    Claushingebjerg
    1

    Im sooooooooo sorry - its my fault!!!!!!

    While debugging i had moved the content of the main partial into the template... loading the main partial instead of having the code in the template fixed the problem.

    I owe you a beer :).

  • Nik 1044 posts 4220 karma points MVP c-trib
    1 week ago
    Nik
    1

    Good to hear you got it sorted :-)

Please Sign in or register to post replies

Write your reply to:

Draft