Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 231 posts 901 karma points c-trib
    Nov 22, 2020 @ 05:30
    Martin Rud
    0

    Extend Model with a property for all document types for use in all views

    I have a property on all document types headline and right now it is used in redundant code in two (could be more) view files:

    Meta data partial view:

    <title>@(Model.Value<string>("headline") != "" ? Model.Value("headline") : Model.Name)</title>
    

    Different template views:

    <h1>@(Model.Value<string>("headline") != "" ? Model.Value("headline") : Model.Name)</h1>
    

    I am now trying to extend the Model so I can use Model.Value("PageTitle) / Model.PageTitle instead.

    But I must admit that I can´t find out. I have done a lot og Razor in views, but not yet worked with my own custom classes i .NET nor Umbraco.

    One thing I have found out, though: This doesn´t work (and also seems to be tied up on only the SiteHome document type)! :)

    namespace Umbraco.Web.PublishedModels
    {
        public partial class SiteHome
        {
            public string PageTitle
            {
                get
                {
                   return Model.Value<string>("headline") != "" ? Model.Value("headline") : Model.Name;
                }
            }
        }
    }
    
  • Marc Goodson 2122 posts 14213 karma points MVP 8x c-trib
    Nov 23, 2020 @ 07:22
    Marc Goodson
    0

    Hi Martin

    You 'used' to be able to specify a base class for all your generated Modelsbuilder Models:

    https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Control-Generation-vpre8_5#models-base-class

    But I think this may have been removed from the Core version of Umbraco in version 8.5, and in order to do this you may require the external plugin:

    https://github.com/dawoe/Zbu.ModelsBuilder

    regards

    Marc

  • Martin Rud 231 posts 901 karma points c-trib
    Nov 23, 2020 @ 11:17
    Martin Rud
    0

    Thanks. Can you point me in a direction with some code? I am newbie in working with classes. 🙂

Please Sign in or register to post replies

Write your reply to:

Draft