Copied to clipboard

Flag this post as spam?

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


  • TooensureMaster 4 posts 25 karma points
    Jun 02, 2021 @ 20:56
    TooensureMaster
    1

    Edit and reference models in Model Builder

    So I finally set up model builder in my project.

    When I press build models button the models or generated.

    But I thought I was able to edit these models/create them on my own?

    Also when I try to reference them I don't know how.

    Within .Web project when createing the Home template for instance.

    I'm trying to access the title.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @using ContentModels = namespcae
    @{
        Layout = null;
    }
    <p>@ContentModels.Home</p>
    

    I cant say @ContentModels.Home because Home is a type.

    Solved

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Home>
    
    @using ContentModels = namespace;
    @{
        Layout = null;
    }
    <p>@Model.Title</p>
    

    Now can I create models myself, obviously I can, but is this best practice vs having them generated.

  • Marc Goodson 2148 posts 14352 karma points MVP 8x c-trib
    Jun 03, 2021 @ 20:09
    Marc Goodson
    0

    Hi TooensureMaster

    The idea with Modelsbuilder is to generate from the DocumentType definition a set of Models that match the properties in the DocumentType 1-1 to make it easy to work with in templates... eg with Modelsbuilder enabled you can write

    @Model.Title instead of @Model.Value("title")

    and if you are using Visual Studio and running Modelsbuilder in AppData mode, you can include the generated files in your Visual Studio project and get intellisense to help discover/remember property names.

    Also if you have the generated models in your Visual Studio project, you can extend them by using Partial classes, or by inheriting from them, or by creating extension methods.

    https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Understand-And-Extend

    For simple sites the generated models, perhaps slightly extended is 'enough' to deliver the associated template...

    ... so you shouldn't need to create your own models...

    But for more complex site templates, it might make sense to create your own 'ViewModel', independent of these generated models, and then you might choose to roll your own - and use a technique called RouteHijacking to populate these custom ViewModels and send them to your views...

    https://our.umbraco.com/documentation/reference/routing/custom-controllers

    In terms of best practice, it will always depend on the context, some people dislike the workflow of generating models using ModelsBuilder and decide to just use IPublishedContent (that's the @Model.Value("alias") approach - if you look at the generated models the properties are just a 'wrapper' around this approach). It can be quicker and more intuitive for some people, and then others prefer to always hijack and create a custom ViewModel, either using the generated model as a base or ignoring them completely. There are upsides and downsides to all the approaches, but there isn't one best way... it depends on what makes sense to you, and your team and the context of the site.

    If that helps!

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft