Copied to clipboard

Flag this post as spam?

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


  • Florian 7 posts 109 karma points
    Apr 03, 2019 @ 13:17
    Florian
    0

    ModelBindingException: Cannot bind source content type

    Hi there,

    I am new to web development and Umbraco. I freshly started with Umbraco v8 Cloud.

    Now I have a question for better understanding of the structure or how does best practice looks like.

    I get always an error message when I start my solution and I don't know how to fix that.

    ModelBindingException: Cannot bind source content type Umbraco.Web.PublishedModels.HomePage to model type Umbraco.Web.PublishedModels.Product.
    

    My solution is running in Modelbuilder AppData mode and contains a master page, a hompage and this homepage is loading several partials for a better overview:

    master.cshtml
    |- homepage.cshtml
        |- partial_1.cshtml
        |- partial_2.cshtml
        |- ..
    

    My master source looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.HomePage>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <head>
        <title>@Model.HomeTitle</title>
    </head><!--/head-->
    
    <body id="home" class="homepage">
    
        @RenderBody()
    
    </body>
    </html>
    

    My homepage source:

    @{
        Layout = "master.cshtml";
    }
    
    @Html.Partial("partial_1")
    @Html.Partial("partial_2")
    

    And my partial_1 source:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Product>
    @using ContentModels = Umbraco.Web.PublishedModels;
    
    <section id="pricing">
        <!-- Want to access Product fields here -- >
    </section>
    

    Whats wrong, where is my mistake?

    How is the best practice to access multiple ContentModels on one page please?

    Thanks Florian

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 03, 2019 @ 20:59
    Alex Skrypnyk
    0

    Hi Florian

    In master, you use "HomePage" model :

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    In the partial you use "Product" model: @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Model type means what document type you are browsing now, the page should be HomePage or Product, the master template should be without type at all I assume, and only document specific views should have a type.

    Try to remove "

    Thanks,

    Alex

  • Florian 7 posts 109 karma points
    Apr 03, 2019 @ 21:07
    Florian
    0

    Hi Alex,

    Thanks for your advise.

    Does that mean I cannot use different models in different partial views or just not in Master and details?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 03, 2019 @ 21:09
    Alex Skrypnyk
    0

    You can use different models.

    But if you want this model types means - what type of page are you loading now.

    In master usually, you can use a type that is a parent for all page types.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 03, 2019 @ 21:10
    Alex Skrypnyk
    0

    If partial is page specific - use page type.

    If partial is using on all pages - do not use model type.

  • Florian 7 posts 109 karma points
    Apr 04, 2019 @ 06:04
    Florian
    0

    Thanks for clarification.

    Do I understand right, that the general policy is one page one model?

    I like the strong typed feature which comes with Model Builder, but if I want multiple models on one View this is only possible with weak types and magic field strings like @Model.Value("field"), right?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 04, 2019 @ 09:41
    Alex Skrypnyk
    100

    Hi Florin

    You can use types and strongly typed models, but the page that is using the current template always only one type.

    On home page Model that is passing to Home page template - always HomePage model.

    You can retrieve any node from Umbraco tree and use strongly typed models with them on the home page, but RenderModel will be HomePage.

    I hope it's possible to understand what I wrote:)

    ask as many questions as possible, this community is awesome

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 10, 2019 @ 15:57
    Alex Skrypnyk
    1

    Florian, let us know did you find a solution?

    Alex

  • Florian 7 posts 109 karma points
    Apr 10, 2019 @ 16:54
    Florian
    1

    Yes, I got rid of the error.

    It is like you said. I can only bind one model to one view.

    If I want strond typed Lists I can do it like this inside a partial view:

    FeatureList featureList = (FeatureList)Umbraco.Content(1061);
    

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft