Copied to clipboard

Flag this post as spam?

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


  • Ken Mark 75 posts 87 karma points
    Sep 29, 2016 @ 10:44
    Ken Mark
    0

    Extend Model using partial class

    I'm not able to create a partial class to extend a generated model. Can anyone help?

    I'm in Dll mode

    I create a class called Startpage.cs and put it in App_data\Models (Not inluded in the projekt)

    It looks like this:

    namespace Umbraco.Web.PublishedContentModels
    {
        public partial class Startpage
        {
            public string TestProp
            {
                get { return "TestProp"; }
            }
        }
    }
    

    In the template i write this:

    <p>@Model.Content.TestProp</p>
    

    Not working...

  • Alex Skrypnyk 5774 posts 21998 karma points MVP 4x admin c-trib
    Sep 29, 2016 @ 13:05
    Alex Skrypnyk
    0

    Hi Ken,

    As I know these classes are generated - you can't create them.

    If you want to return a custom model for Umbraco page, look here - https://github.com/kgiszewski/LearnUmbraco7/blob/master/Chapter%2006%20-%20Surface,%20WebAPI%20and%20RenderMVC%20Controllers/01%20-%20RenderMVC%20Controllers.md

    And ask, ask, ask !!! :)

    Thanks,

    Alex

  • Ken Mark 75 posts 87 karma points
    Sep 29, 2016 @ 13:26
    Ken Mark
    1

    Hi, according this text it should be possible

    "Because a model is generated as a partial class, it is possible to extend it, eg adding a property, by dropping the following code in a MyDocument.cs file:"

    from this page

    https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Understand-And-Extend

  • Alex Skrypnyk 5774 posts 21998 karma points MVP 4x admin c-trib
    Sep 29, 2016 @ 14:12
    Alex Skrypnyk
    0

    Thanks, Ken, didn't use it like that.

  • Chris Evans 114 posts 285 karma points c-trib
    Mar 21, 2017 @ 01:08
    Chris Evans
    0

    @Ken - did you manage to resolve this at all? Am encountering the same problem with my v7.5.9 installation, running ModelsBuilder in Dll mode.

  • John C Scott 457 posts 1157 karma points
    Mar 26, 2018 @ 15:26
    John C Scott
    0

    I've had this working in previous versions of Umbraco but doesn't seem to want to work in my first 7.9 site, is this a breaking change?

  • John Bergman 459 posts 1065 karma points
    Mar 26, 2018 @ 19:36
    John Bergman
    0

    I do this all the time (Including 7.9.x), but I target the generated classes into a seperate assembly, and then add partial classes there.

    The big thing is that the namespace needs to be the same in both classes for it to work correctly.

  • John C Scott 457 posts 1157 karma points
    Mar 26, 2018 @ 20:13
    John C Scott
    0

    Ok yes thank you I see that's the answer. How do you define that? Which mode is that? How do you say what assembly you way it to be in?

  • David Brendel 774 posts 2843 karma points c-trib
    Mar 26, 2018 @ 20:24
    David Brendel
    2

    Hi John,

    you can define the namespace that models builder is using for the generated classes:

    Umbraco.ModelsBuilder.ModelsNamespace (string, default is Umbraco.Web.PublishedContentModels) specifies the generated models namespace.
    

    Then you can use that namespace also on your partial classes and it should pick it up.

    Regards David

Please Sign in or register to post replies

Write your reply to:

Draft