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.
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 :-)
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 aTextString
namedEmail
.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:
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
andUmbracoViewPage
as opposed toAppData
andUmbracoTemplatePage
which is what I am using.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
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 :-)is working on a reply...