Copied to clipboard

Flag this post as spam?

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


  • Dean 13 posts 123 karma points
    Mar 28, 2023 @ 20:57
    Dean
    0

    Strongly typed model

    Hi

    I've created a new class Library and moved all the models into it using the model builder and configuring the JSON file to move it to an external class library using Umbraco 10 and .Net 6.

    In my website project (Umbraco 10) I reference the class library (called Umbraco.Models).

    In my template (razor) how should I access the page/Doc type created? When I write

    UmbracoViewPage

    Models does not exist (I've added project reference). ContentModels.Home does exist?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 29, 2023 @ 07:39
    Huw Reddick
    0

    Hi Dean,

    You should just be able to do

    @inherits UmbracoViewPage<Home>
    
  • Dean 13 posts 123 karma points
    Mar 29, 2023 @ 09:32
    Dean
    0

    Hi

    Interestingly that worked!

    Maybe I misunderstood something here but I was trying a fully qualified name.

    @inherits UmbracoViewPage<Home>
    

    This worked!

    But this didn't

    @inherits UmbracoViewPage<Umbraco.Models.Home>
    

    My models sit in a separate class library (so Home is a class declared as Home.generated.cs.

    When I hover over the code you supplied that works, I notice Home is Umbraco.CMS.Web.Common.PublishedModels.Home

    So is this mapping to the same object or in a polite way did you forget I mentioned my models are in a separate class library?

    Cheers

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 29, 2023 @ 10:37
    Huw Reddick
    100

    it will still belong to Umbraco.CMS.Web.Common.PublishedModels unless you change the namespace in the modelsbuilder settings "ModelsNamespace"

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Mar 29, 2023 @ 12:37
    Nik
    1

    Hi Dean,

    As Huw says,

    The models are being generated in your own class library, that is correct, but unless you use the setting to set the models namespace they will still be using the default namespace of Umbraco.CMS.Web.Common.PublishedModels.

    https://docs.umbraco.com/umbraco-cms/reference/configuration/modelsbuildersettings

    The setting you need if you want to change their namespace is the "ModelsNamespace" property.

    Thanks

    Nik

  • Dean 13 posts 123 karma points
    Mar 29, 2023 @ 13:43
    Dean
    0

    No thanks Nik, they can remain in the same library/naming convention but I was confused thinking my class library isn't working the way it's supposed to but you guys have explained that well.

    So even though it's in a separate library I can continue to use the same path which is nice.

Please Sign in or register to post replies

Write your reply to:

Draft