The idea with Modelsbuilder is to generate from the DocumentType definition a set of Models that match the properties in the DocumentType 1-1 to make it easy to work with in templates... eg with Modelsbuilder enabled you can write
@Model.Title
instead of
@Model.Value("title")
and if you are using Visual Studio and running Modelsbuilder in AppData mode, you can include the generated files in your Visual Studio project and get intellisense to help discover/remember property names.
Also if you have the generated models in your Visual Studio project, you can extend them by using Partial classes, or by inheriting from them, or by creating extension methods.
For simple sites the generated models, perhaps slightly extended is 'enough' to deliver the associated template...
... so you shouldn't need to create your own models...
But for more complex site templates, it might make sense to create your own 'ViewModel', independent of these generated models, and then you might choose to roll your own - and use a technique called RouteHijacking to populate these custom ViewModels and send them to your views...
In terms of best practice, it will always depend on the context, some people dislike the workflow of generating models using ModelsBuilder and decide to just use IPublishedContent (that's the @Model.Value("alias") approach - if you look at the generated models the properties are just a 'wrapper' around this approach). It can be quicker and more intuitive for some people, and then others prefer to always hijack and create a custom ViewModel, either using the generated model as a base or ignoring them completely. There are upsides and downsides to all the approaches, but there isn't one best way... it depends on what makes sense to you, and your team and the context of the site.
Edit and reference models in Model Builder
So I finally set up model builder in my project.
When I press build models button the models or generated.
But I thought I was able to edit these models/create them on my own?
Also when I try to reference them I don't know how.
Within .Web project when createing the Home template for instance.
I'm trying to access the title.
I cant say
@ContentModels.Home
because Home is a type.Solved
Now can I create models myself, obviously I can, but is this best practice vs having them generated.
Hi TooensureMaster
The idea with Modelsbuilder is to generate from the DocumentType definition a set of Models that match the properties in the DocumentType 1-1 to make it easy to work with in templates... eg with Modelsbuilder enabled you can write
@Model.Title instead of @Model.Value("title")
and if you are using Visual Studio and running Modelsbuilder in AppData mode, you can include the generated files in your Visual Studio project and get intellisense to help discover/remember property names.
Also if you have the generated models in your Visual Studio project, you can extend them by using Partial classes, or by inheriting from them, or by creating extension methods.
https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Understand-And-Extend
For simple sites the generated models, perhaps slightly extended is 'enough' to deliver the associated template...
... so you shouldn't need to create your own models...
But for more complex site templates, it might make sense to create your own 'ViewModel', independent of these generated models, and then you might choose to roll your own - and use a technique called RouteHijacking to populate these custom ViewModels and send them to your views...
https://our.umbraco.com/documentation/reference/routing/custom-controllers
In terms of best practice, it will always depend on the context, some people dislike the workflow of generating models using ModelsBuilder and decide to just use IPublishedContent (that's the @Model.Value("alias") approach - if you look at the generated models the properties are just a 'wrapper' around this approach). It can be quicker and more intuitive for some people, and then others prefer to always hijack and create a custom ViewModel, either using the generated model as a base or ignoring them completely. There are upsides and downsides to all the approaches, but there isn't one best way... it depends on what makes sense to you, and your team and the context of the site.
If that helps!
regards
marc
is working on a reply...