I want to have a typed access to the model behind my pages in views. I see there is a models.generated.cs file in App_Data/Models, but I'm not sure if the types inside it are very accessible.
In other words, if I'm in the view of a page of type HomePage and I have a true/false field called Dummy for its doc type, is there a way to have something like Model.Dummy that is of type bool, and gets displayed by intellisense (since it is not dynamic)?
Version 7.5+ has the Model Builder as part of the code. As a result there are numerous options for this awesome feature. The one I use when developing is to change the following setting in web.config:
I set the value to Dll and then each time I make changes to the document types I go to the Models Builder dashboard and tell it to generate a new DLL. This updates the Modes DLL and as a result when I'm editing views I have a nicely typed model with intelisense.
You might find that this config option isn't quite what you are after, but there are various other options. (hopefully someone can give the official link to the documentation as I can't seem to find it)
What you will notice is that the properties are all off of the "Content" property of the Model, but effectively this should do what you are after.
This should solve many of the problems, but not all of them. I want to have some "generated" get-only properties in my view models, and of course, I would like to keep them in my model class, not view. For example, I have an Abstract property, which is a string, and I would like to have a get-only TrancatedAbstract property, and I don't want to bring the logic to view.
It's very sad that model exposure is not available in Umbraco by default. Many of the Umbraco code samples that I'm seeing is a spaghetti inline mixture of model creation logic and rendering code in the .cshtml views, which clearly violates the "separation of concerns" principle.
Thanks again, and I really don't know why there are no official documentations on this Model Builder! :D
If you look at the wiki link here: https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Understand-And-Extend it talks about extending the models and adding your own properties to it. I've had a bit of a read of it but I'm yet to try and implement it myself so it could be worth investigating this to achieve what you are after.
Access models.generated.cs
Hi,
I want to have a typed access to the model behind my pages in views. I see there is a models.generated.cs file in App_Data/Models, but I'm not sure if the types inside it are very accessible.
In other words, if I'm in the view of a page of type HomePage and I have a true/false field called Dummy for its doc type, is there a way to have something like Model.Dummy that is of type bool, and gets displayed by intellisense (since it is not dynamic)?
Hi Siavash,
Version 7.5+ has the Model Builder as part of the code. As a result there are numerous options for this awesome feature. The one I use when developing is to change the following setting in web.config:
I set the value to Dll and then each time I make changes to the document types I go to the Models Builder dashboard and tell it to generate a new DLL. This updates the Modes DLL and as a result when I'm editing views I have a nicely typed model with intelisense.
I can then use the model like this:
You might find that this config option isn't quite what you are after, but there are various other options. (hopefully someone can give the official link to the documentation as I can't seem to find it)
What you will notice is that the properties are all off of the "Content" property of the Model, but effectively this should do what you are after.
:-)
Nik
Thank you Nik,
Oh, I see!
This should solve many of the problems, but not all of them. I want to have some "generated" get-only properties in my view models, and of course, I would like to keep them in my model class, not view. For example, I have an Abstract property, which is a string, and I would like to have a get-only TrancatedAbstract property, and I don't want to bring the logic to view.
It's very sad that model exposure is not available in Umbraco by default. Many of the Umbraco code samples that I'm seeing is a spaghetti inline mixture of model creation logic and rendering code in the .cshtml views, which clearly violates the "separation of concerns" principle.
Thanks again, and I really don't know why there are no official documentations on this Model Builder! :D
Cheers, Sia
Hi Siavash,
There is a way to extend the models, granted I'm not 100% on it. The models are all partial classes.
The model builder in Umbraco is actually based on this one: https://github.com/zpqrtbnk/Zbu.ModelsBuilder
If you look at the wiki link here: https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Understand-And-Extend it talks about extending the models and adding your own properties to it. I've had a bit of a read of it but I'm yet to try and implement it myself so it could be worth investigating this to achieve what you are after.
Nik
Hi Nik
Thank you very much for sharing these. I think that article is a very good starting point.
Cheers, Sia
is working on a reply...