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 261 posts 1002 karma points c-trib
    Aug 13, 2022 @ 11:38
    Martin Rud
    0

    How to enable Modelsbuilder? Models seems to be generated

    Hi,

    How to enable Modelsbuilder? Models seems to be generated, but using Model.PropertyName returns "...does not contain a definition for..."

    I have never used Models builder before and my question might reflect that: In umbraco\Data\TEMP\InMemoryAuto\models.generated.cs I can see that they seem to be generated. For instance my property "headline":

        ///<summary>
        /// Overskrift (valgfri)
        ///</summary>
           [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "10.1.0+3972538")]
            [global::System.Diagnostics.CodeAnalysis.MaybeNull]
            [ImplementPropertyType("headline")]
            public virtual string Headline => global::Umbraco.Cms.Web.Common.PublishedModels.SiteComp_ContentBase1.GetHeadline(this, _publishedValueFallback);
    

    But calling this from a template:

    <h1>@Model.Headline</h1>
    

    returns:

    "'IPublishedContent' does not contain a definition for 'Headline' and no accessible extension method 'Headline' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)"
    

    Reason that I want to use ModelsBuilder is that I want to use the package "Nikcio.UHeadless" (https://github.com/nikcio/Nikcio.UHeadless/tree/v2/contrib) where I need the models for my graphql queries. For example this query fails due to (I guess) missing model:

    enter image description here

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 14, 2022 @ 10:22
    Marc Goodson
    1

    Hi Martin

    In your appsettings.json file you can control the settings for Modelsbuilder

    And it depends on how you are working with Umbraco that would define how you configure this.

    But if you are working with Visual Studio, and you have multiple supporting Class Library projects then you 'probably' want to use SourceCodeAuto, and specify the directory to generate the models to be inside a specific Class Library project so they can easily be shared without circular dependencies eg:

        "ModelsBuilder": {
                "ModelsMode": "SourceCodeAuto",
                "AcceptUnsafeModelsDirectory": true,
                "ModelsDirectory": "~/../YourSupportingClassLibrary/Models/CmsModels",
                "ModelsNamespace": "YourSupportingClassLibrary.Models.CmsModels",
                "DebugLevel": 0
    
            },
    

    Now when DocTypes change when running locally, they'll be updated inside this project, they'll be included in your Visual Studio solution, and therefore enable intellisense!

    But that's not it!

    When you are inside a View/Template you need to tell it to use these generated Models, with Modelsbuilder turned on, when you create a new View, Umbraco will automatically wire this up for you!

    It's all done in the inherits statement at the top of the view eg

    if you have

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    

    Then the view is expecting the Model to be of type IPublishedContent (it works because your generated models all implement IPublishedContent - but IPublishedContent doesn't know about your Headline property.

    If you, therefore, update the inherits statement to use the generated model for your DocType for that view, eg for a Homepage doctype and template this would look like this:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Homepage>
    

    Then your Model would have the Headline property eg

    <h2>@Model.Headline</h2>
    

    You would need to add the namespace of your generated models to your _ViewImports.cshtml file, to allow them to be referenced in each view.

    Haven't used the UHeadless package that you refer to, but hopefully if you have the models generated and working in a view, then that's all it needs to...

    regards

    marc

  • Martin Rud 261 posts 1002 karma points c-trib
    Aug 22, 2022 @ 06:05
    Martin Rud
    0

    Thanks for the suggestion. It solves the problem for the views where @Model.Headline now works.

    But in the UHeadless package, unfortunately, there is no change.

Please Sign in or register to post replies

Write your reply to:

Draft