Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    May 13, 2019 @ 12:02
    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 122 posts 362 karma points
    May 13, 2019 @ 12:16
    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 939 posts 2574 karma points
    May 13, 2019 @ 12:21
    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 122 posts 362 karma points
    May 13, 2019 @ 12:32
    Magnus Eriksson
    1

    Try switching IPublishedContent to IPublishedElement

    Regards, Magnus

  • Sören Deger 733 posts 2844 karma points c-trib
    May 13, 2019 @ 12:32
    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 939 posts 2574 karma points
    May 13, 2019 @ 12:37
    Claushingebjerg
    0

    My template:

    enter image description here

    My nested content partial(s):

    enter image description here

  • Magnus Eriksson 122 posts 362 karma points
    May 13, 2019 @ 12:41
    Magnus Eriksson
    0

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

    Regards, Magnus

  • Claushingebjerg 939 posts 2574 karma points
    May 13, 2019 @ 12:42
    Claushingebjerg
    1

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

  • Claushingebjerg 939 posts 2574 karma points
    May 13, 2019 @ 12:48
    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 1625 posts 7295 karma points MVP 7x c-trib
    May 13, 2019 @ 16:06
    Nik
    102

    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 939 posts 2574 karma points
    May 13, 2019 @ 17:21
    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 1625 posts 7295 karma points MVP 7x c-trib
    May 13, 2019 @ 18:05
    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 939 posts 2574 karma points
    May 13, 2019 @ 18:27
    Claushingebjerg
    0

    enter image description here

    enter image description here

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    May 14, 2019 @ 07:41
    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 939 posts 2574 karma points
    May 14, 2019 @ 07:54
    Claushingebjerg
    0

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

  • Claushingebjerg 939 posts 2574 karma points
    May 15, 2019 @ 08:04
    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 1625 posts 7295 karma points MVP 7x c-trib
    May 15, 2019 @ 08:21
    Nik
    1

    Good to hear you got it sorted :-)

  • 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