MortenMoulder
MortenMoulder

Reputation: 6648

Using Strongly Typed models in Umbraco 7.6.9?

I have tried looking into using Strongly Typed models (setting Umbraco.ModelsBuilder.ModelsMode to either AppData or Dll) for a while now, and I never fully understood how it works.

I already changed the Umbraco.ModelsBuilder.ModelsMode value and I generated the models inside the backoffice ModelsBuilder, then I included the App_Data\Models into Visual Studio, but what then?

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Home>
@using Our.Umbraco.Vorto.Models;
@using Our.Umbraco.Vorto.Extensions;
@using ContentModels = Umbraco.Web.PublishedContentModels;

This is the code for my Home view. No matter what I try, I cannot access the @Model.PROPERTY or @CurrentPage.PROPERTY from my content. I can see the different properties inside my MODEL.generated.cs files.

What steps do I need to take, in order to do things like @Model.PROPERTY?

Upvotes: 1

Views: 1569

Answers (2)

Marcin Zajkowski
Marcin Zajkowski

Reputation: 1728

You're not talking about "Dynamic" models, but Strongly Typed Models generated by Models Builder. By default Umbraco ships with PureLive setting which is keeping models in memory and generates them on the fly. It can be considered as "dynamic".

The tool and it's behaviour is well documented here: https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki

Regarding modes of it, you can find all about it exactly in this place: https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Builder-Modes

But answering your question - after you've changed the configuration, you need to compile your application as you need to keep those classes inside the DLL with which you're shipping your website. You're also able to regenerate models straight from your Developer's dashboard in Umbraco Backoffice.

You need to remember that if you would like to use DLL, LiveDLL or PureLive configuration - you need to get rid of classes generated inside your AppData or any other directory used with this mode as you'll experience errors saying about 'More that one type want to be a model for content type File'.

After that you should be able to access all properties of the document type via Model.Content.PropertyAlias. You probably missed the Content object, which is the strongly typed, IPublishedContent representation of you document.

Hope it will help you to make it work :)

Upvotes: 1

MortenMoulder
MortenMoulder

Reputation: 6648

Okay, so it seems like there has been some changes in the newest 7.6.9 release (or maybe 7.6.8). This is what I had to do now:

<add key="Umbraco.ModelsBuilder.Enable" value="true" />
<add key="Umbraco.ModelsBuilder.ModelsMode" value="Dll" />

Then I can go into the backoffice and generate the models. The models are included into the project (location: ~\App_Data\Models\). Umbraco.Web.PublishedContentModels.dll from the ~\bin\ folder has to be included as well.

Then, because of .NET Core I think, I got an error when I tried loading my application saying this:

More than one type want to be a modle for content type File

This was caused because I had included everything inside ~\bin\, which means I had also included my Project.dll, Project.dll.config, and Project.pdb files. The Project.dll file also includes the same models, apparently, so I had to exclude those 3 files.

Now it simply works and I can now do @Model.Content.PROPERTY flawlessly.

Upvotes: 1

Related Questions