Copied to clipboard

Flag this post as spam?

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


  • Siavash Mortazavi 24 posts 145 karma points
    Oct 07, 2016 @ 13:39
    Siavash Mortazavi
    0

    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)?

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Oct 07, 2016 @ 14:16
    Nik
    100

    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:

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

    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:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Event>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = null;
    
        <div>@Model.Content.GeneralName</div>
        <div>@Model.Content.Description</div>
    

    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

  • Siavash Mortazavi 24 posts 145 karma points
    Oct 11, 2016 @ 17:54
    Siavash Mortazavi
    0

    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

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Oct 11, 2016 @ 18:05
    Nik
    0

    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

  • Siavash Mortazavi 24 posts 145 karma points
    Oct 13, 2016 @ 11:50
    Siavash Mortazavi
    1

    Hi Nik

    Thank you very much for sharing these. I think that article is a very good starting point.

    Cheers, Sia

Please Sign in or register to post replies

Write your reply to:

Draft