Copied to clipboard

Flag this post as spam?

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


  • David Hochuli 8 posts 99 karma points
    Apr 10, 2019 @ 09:42
    David Hochuli
    0

    Render Element Problems

    Hi @all!

    I have some issues with rendering element types.

    Maybe someone of you does have some advice for me?

    I get the following error:

    Cannot bind source type Umbraco.Web.PublishedModels.SwiperCollection to model type Umbraco.Core.Models.PublishedContent.IPublishedContent. The source is a ModelsBuilder type, but the view model is not. The application is in an unstable state and should be restarted.
    

    This is my code:

    @{
    var items = Model.Value<IEnumerable<IPublishedElement>>("bodyContent").ToList();
    foreach(var item in items) {
        if (item.ContentType.Alias == "swiperCollection") {
            @Html.Partial("swiperComponentLayout", item);
        }
    }
    

    }

    This is my partial:

    @inherits UmbracoViewPage<ContentModels.SwiperCollection>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @using Umbraco.Web.Models
    @{
        Layout = "BaseComponent.cshtml";
    }
    

    I'm just don't get the correct way to render it. Please help..:) Thank you in advance! Cheers David

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 10, 2019 @ 12:15
    Corné Strijkert
    1

    Hi David,

    Is your intention to render Nested Content items? If so I think the issue is that the SwiperCollection class doesn't inherit form the PublishedElementModel (IPublishedElement) class. Instead it inherit from the PublishedContentModel (IPublishedContent) which causes the error.

    Umbraco 8 introduces the special interface IPublishedElement for Nested Content items.

    You are casting the 'bodyContent' to IEnumerable<IPublishedElement>, so it seems that you are using Nested Content in the 'bodyContent' property.

    On the SwiperCollection content type in the Umbraco UI you can set the setting 'Is Element type' to true.

    Is Element Type setting on Content Type

    Then when you rebuild your ModelsBuilder models you will see that your SwiperCollection class is inheriting the PublishedElementModel class like this:

    public partial class SwiperCollection : PublishedElementModel 
    {
    
    }
    

    Moreover when using Nested Content and Modelsbuilder it better to use the the following code to render your partial views:

    foreach(var item in Model.BodyContent) {
        if (item.ContentType.Alias == SwiperCollection.ModelTypeAlias) {
            @Html.Partial("swiperComponentLayout", item);
        }
    }
    

    When using the Element types correctly the 'BodyContent' property on your model contains automatically an IEnumerable<SwiperCollection>

  • David Hochuli 8 posts 99 karma points
    Apr 10, 2019 @ 13:12
    David Hochuli
    0

    Hi Corné,

    Thank you for your answer! Unfortunately it is still not working..

    You are right, my 'bodyContent' is a NestedContent.

    This is my structure: enter image description here

    My DefaultPage is using the Nested Content 'Nested Content': enter image description here

    The used document type 'SwiperCollection' is also using Nested Content: enter image description here

    Then the Model:

    public partial class SwiperCollection : BaseComponent
    {
    }
    
    public partial class BaseComponent : PublishedElementModel
    {
    }
    

    My Templates: DefaultPage:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.DefaultPage>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        Layout = "master.cshtml";
    }
    
    @{
    foreach(var item in Model.BodyContent) {
        if (item.ContentType.Alias == SwiperCollection.ModelTypeAlias) {
            @Html.Partial("swiperComponentLayout", item);
        }
    }
    

    SwiperComponentLayout:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.SwiperCollection>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        Layout = "BaseComponent.cshtml";
    }
    
    <p>Hello World</p>
    

    With all this I get the following error: enter image description here

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 10, 2019 @ 14:04
    Corné Strijkert
    1

    Just double check, are you sure that you have toggled the 'Is an Element type' setting to true on the Swiper Collection and Article document type by going to Permissions --> Is an Element type? This is a really important setting!

    Your SwiperCollection class is not directly inheriting PublishedElementModel, but BaseComponent. I think this is your parent document type? If so, double check if the 'Is an Element type' setting on this document type is set to true as well.

    How do you generate the models. What is your configuration? AppData, Dll or API? I have the same configuration and this works fine. Maybe you have to rebuild your projects or copy the contentmodels assembly to the right locations.

  • David Hochuli 8 posts 99 karma points
    Apr 10, 2019 @ 14:12
    David Hochuli
    0

    Yes, SwiperCollection, Article and BaseComponent are all set as Element type: enter image description here enter image description here

    I generate my models through AppData. I have already rebuilt the project. What do you mean by 'copy the content models assembly to the right locations'?

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 10, 2019 @ 14:32
    Corné Strijkert
    1

    Interesting!

    I was trying to reproduce your error in my Umbraco 8 solution. I built the exact same setup, but no problems occur. Only when I change

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.SwiperCollection>
    

    to:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    in my partial view I will get the same error.

    Are you sure the SwiperComponentLayout partial is correctly saved etc? In your partial you are using the BaseComponent.cshtml layout page. What is in that view? Maybe you are inheriting something IPublishedContent stuff in that view?

    Edit: I have tried to add the parent layout to my partial view without the model specification and BAM! So maybe that's the problem in you solution too?

  • David Hochuli 8 posts 99 karma points
    Apr 10, 2019 @ 14:40
    David Hochuli
    0

    My BaseComponent.cshtml is almost empty: enter image description here

    But it is not in the folder Partial Views. Can this be the problem?

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 10, 2019 @ 14:44
    Corné Strijkert
    100

    For sure!

    Also the @RenderBody() is not going to work for partial views I think?

    Removing the Layout = "BaseComponent.cshtml" part in your partial view is the solution I think

  • David Hochuli 8 posts 99 karma points
    Apr 10, 2019 @ 14:53
    David Hochuli
    0

    You are absolutely right!

    It is now rendering the partial view. But I get the next error..

    enter image description here

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 10, 2019 @ 16:39
    Corné Strijkert
    1

    I don't know where @item comes from? It is a child object or the current model? Or?

    Sometimes in Razor views the red highlighted line is not the code that causes the error, so I'd like to recommend you to debug this view to get info about where things go wrong.

  • David Hochuli 8 posts 99 karma points
    Apr 11, 2019 @ 05:42
    David Hochuli
    1

    Thank you so much for your help Corné! If I delete this <p> tag it is working. I will check this problem, but the rendering of the partial view is working! :)

Please Sign in or register to post replies

Write your reply to:

Draft