Copied to clipboard

Flag this post as spam?

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


  • Darryl 31 posts 112 karma points
    Nov 17, 2018 @ 16:01
    Darryl
    0

    Extending Models Builder in AppData Mode

    I am trying to extend one of my generated models with a simple method called addToEmail() that adds some text to a TextString named Email.

    My Site Models (generated) are in a separate Class library (Umbraco.SiteModels) and my extended model is in another Class library (Umbraco.Web.ViewModels). The latter is referencing SiteModels, and my main project library is referencing the both.

    However, when I create my partial class, I'm getting no IntelliSense and a red line under Email. I have also tried using the namespace for my Site Models and other Umbraco namespaces, but still no luck.

    Here is my code:

    namespace Umbraco.Web.ViewModels
    {
        public partial class HomePage
        {
            public string addToEmail()
            {
                return this.Email + " hello";
            }
        }
    }
    

    I did this after following a tutorial on Umbraco TV, but the project was setup slightly different to mine, and he was using Models builder API and UmbracoViewPage as opposed to AppData and UmbracoTemplatePage which is what I am using.

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Nov 18, 2018 @ 12:54
    Marc Goodson
    1

    Hi Darryl

    That sounds like your generated models, and the class you are using to extend, are in different namespaces...

    For partial classes to work and be considered 'the same class' and so to become combined, they need to be in the same namespace.

    Otherwise they are effectively 'different classes' and c# won't know to combine them just because they have the same class name.

    I'm not sure I've explained that very well! - there is a link here that probably does a better job :-)

    http://www.tutorialsteacher.com/csharp/csharp-partial-class

    regards

    Marc

  • Darryl 31 posts 112 karma points
    Nov 18, 2018 @ 17:07
    Darryl
    0

    Thanks Marc,

    It did dawn on me after being away from my desk that I should of tried to create the partial class within the generated Models folder to see if that was the issue.

    I was hoping I could keep them separate, but even changing the namespace in my Umbraco.Web.ViewModels to the same as the generated models, VS still complained, so it looks as though partial classes should be in the same directory as well as the same namespace.

    All is working and I have successfully added a few methods to manipulate the Email property :-)

Please Sign in or register to post replies

Write your reply to:

Draft