Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 231 posts 901 karma points c-trib
    Jun 01, 2023 @ 12:40
    Martin Rud
    0

    Why, and then how to, use models builder?

    My qustion falls in to parts:

    1. Why should I use it? I have never used strongly typed models, but just used Model.Value("xxx"). But I guess there are reason to why models builder exist, i.e. that there are benefits from using it.
    2. How should I use it? I have figured out how to use it, I think. It works, anyway, an renders the desired content.

    Template PageHome.cshtml

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.PageHome>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @{
        Layout = "Master.cshtml";
    
        var TestComp1 = Model.TestComp1?.ToString();
        <p>@TestComp1</p>
    }
    

    Template PageDefault.cshtml

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.PageDefault>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @{
        Layout = "Master.cshtml";
    
        var TestComp1 = Model.TestComp1?.ToString();
        <p>@TestComp1</p>
    
        var TestComp2 = Model.TestComp2?.ToString();
        <p>@TestComp2</p>
    }
    

    Properties TestComp1 and TestComp2 comes from two separate compositions and TestComp1 is added to document type PageHome while both compositions have been added to PageDefault.

    Then in template PageHome.cshtml I insert a partial view:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<PageHome>
    @{
        var TestComp1 = Model.TestComp1?.ToString();
        <p>@TestComp1</p>
    }
    

    This works fine, but if I insert it intro template PageDefault I get an error:

    ModelBindingException: Cannot bind source content type Umbraco.Cms.Web.Common.PublishedModels.PageDefault to model type Umbraco.Cms.Web.Common.PublishedModels.PageHome.

    And it makes sense, for sure. But is there no way that a partial can be used in templates where the model isn't sthe same? I seems inflexible to me since I don that often in the "old" way without models builder.

  • Ambert van Unen 175 posts 817 karma points c-trib
    Jun 01, 2023 @ 14:24
    Ambert van Unen
    100

    You can do several things, like if you know the partial only contains data from a composition you can just use that as the model:

    Umbraco.Cms.Web.Common.Views.UmbracoViewPage<IPageComposition>

    Or you can make ViewComponents instead of partials with your own created Model just for your needs for that View.

    As to being a big pro for ModelsBuilder:

    ModelsBuilder generates strongly typed models based on your Umbraco document types, ensuring that you have compile-time safety and intellisense support. This means you can access properties and content values with confidence, reducing the risk of runtime errors.

  • Martin Rud 231 posts 901 karma points c-trib
    Jun 02, 2023 @ 03:12
    Martin Rud
    0

    Thanks! ViewComponents seem to be the way to solve what I was looking for. :)

Please Sign in or register to post replies

Write your reply to:

Draft