Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Aug 07, 2019 @ 05:18
    jake williamson
    0

    how to use a modelsbuilder base class with nested content in v8?

    hey out there,

    we're building a new site in v8 that uses nested content and the modelsbuilder. nice.

    in v7 projects we had a base class that all our generated models build up from e.g.

    [assembly: ModelsBaseClass(typeof(BaseModel))]
    namespace OurSite.Core.Models
    {
        public class BaseModel : PublishedContentModel
        {
            public BaseModel(IPublishedContent content) : base(content)
            {
            }
    
            public string PageTitle => this.HasValue("title") ? this.GetPropertyValue<string>("title") : Name;
        }
    }
    

    so we tried to drop this into our shiney new v8 project and everything was running sweet right up until the moment we added a nested content item...

    turns out the models generator creates the model as PublishedElementModel e.g.

    public partial class ImageCarouselItem : PublishedElementModel
    

    so we build our models and not surprisingly get the following error:

    enter image description here

    is there a way we can tell the modelsbuilder or umbraco to ignore doctypes that are elements?

    any suggestions would be grand ;)

    cheers,

    jake

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Aug 08, 2019 @ 00:35
    Peter Gregory
    0

    Has anyone done this? It is super important for v8 and models builder that this works. There doesn't seem to be any documentation anywhere.

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Aug 08, 2019 @ 00:52
    Peter Gregory
    1

    Currently still an issue in Models Builder. This really needs to be sorted ASAP.

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder/issues/205

  • jake williamson 207 posts 873 karma points
    Aug 08, 2019 @ 08:19
    jake williamson
    1

    just spent some time looking at this with peter and a pull request is in ;)

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder/pull/211

    this pr solves the base class issue for nested content items, it'd be good to look at the 'ElementModelsBaseClass' idea mentioned in https://github.com/zpqrtbnk/Zbu.ModelsBuilder/issues/205

    nice.

  • jake williamson 207 posts 873 karma points
    Aug 08, 2019 @ 08:42
    jake williamson
    1

    boom - part two knocked out too ;)

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder/issues/205

    [assembly: ElementModelsBaseClass(typeof(ElementBaseModel))]
    

    is a go. even nicer.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Aug 08, 2019 @ 08:50
    Nik
    0

    I have nothing to contribute here other than nice work to you both! I wouldn't have known where to start with it, but good to hear there are PR's to get it fixed now :-D

  • Travis Schoening 47 posts 173 karma points
    Mar 25, 2021 @ 15:16
    Travis Schoening
    0

    How were you able to get this to work? It doesn't seem to be included in ModelsBuilder 8.1.6?

  • jake williamson 207 posts 873 karma points
    Mar 26, 2021 @ 00:46
    jake williamson
    1

    hi travis,

    so, the trick is that the models builder in the most recent versions of umbraco is actually embedded rather than brought in as a nuget package...

    https://our.umbraco.com/documentation/reference/templating/modelsbuilder/introduction#installing

    The Models Builder is by default embedded in Umbraco. If you need any complex features, you still need to add the full package. Check the official releases on the Models Builder GitHub repository for more details.

    from looking at the pull requests, we're not 100% sure that the updated code was ever merged in and released...

    tbh i've ditched the base class idea in a majority of the projects i'm working on as it was to much hassle to get working and i've since gone with other ideas...

    however, if you do still need it, you can download the fork that we worked in to solve the problem, build it and then reference the dll:

    https://github.com/pgregorynz/Zbu.ModelsBuilder

    hope that helps ;)

    cheers,

    jake

  • Travis Schoening 47 posts 173 karma points
    Mar 27, 2021 @ 14:53
    Travis Schoening
    1

    Jake,

    Appreciate the response. I did exactly this and for my purposes, it at least gets things working. My issue was that a base class was being added to elements, which was preventing the project from building.

    Thanks again! Travis

Please Sign in or register to post replies

Write your reply to:

Draft