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 82 posts 94 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 6132 posts 23951 karma points MVP 7x 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 82 posts 94 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 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 29, 2016 @ 14:12
    Alex Skrypnyk
    0

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

  • Chris Evans 137 posts 353 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 473 posts 1183 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 483 posts 1132 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 473 posts 1183 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 792 posts 2970 karma points MVP 3x 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