Copied to clipboard

Flag this post as spam?

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


  • Pathini 15 posts 105 karma points
    Mar 20, 2024 @ 15:47
    Pathini
    0

    Partial View failing to read Name

    Hi , I have header Partial View to set page header image and title and I have called this inside master.cshtml

    I'm trying to read Page Title if not entered assign Name and add to Header I received below error , how do I resolve this please ?

    type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'method group'

     @inherits UmbracoViewPage<IHeaderProperties>
    

    @model IPublishedContent

    @{
        var _headerbackground = Model.BackgroundImage!=null ? Model.BackgroundImage.Url() : "/media/wixp33pk/ic-background-2.png";
        var _title =  Model?.Title != null  ? Model.Title : Model.Name  ;
    
    
    
    }
        <!-- Page Header-->
        <header class="masthead" style="background-image: url('@_headerbackground')">
            <div class="container position-relative px-4 px-lg-5">
                <div class="row gx-4 gx-lg-5 justify-content-center">
                    <div class="col-md-10 col-lg-8 col-xl-7">
                        <div class="site-heading">
                            <h1>@_title</h1>
                            <span class="subheading">@Model.SubTitle</span>
                        </div>
                    </div>
                </div>
            </div>
        </header>
    
  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Mar 26, 2024 @ 08:55
    Marc Goodson
    0

    Hi Pathini

    You can't have both the @inherits and the @model statement in the partial view, if you keep with your

    @inherits UmbracoViewPage<IHeaderProperties>
    

    Then this is making 'Model' be assigned to the IHeaderProperties interface, which is why your PageTitle (which isn't on your HeaderProperties composition, can't be accessed via Model.PageTitle)... but all is not lost.

    As you have inherited from UmbracoViewPage, that also gives you access to the current page, via Umbraco.AssignedContentItem

    so you could have

    var currentPage = Umbraco.AssignedContentItem;
    var _title = currentPage.HasValue("title") ? currentPage.Value("title") : currentPage.Name;
    

    or if your PageTitle is on a common composition on all your Document Types perhaps called 'CommonPageProperties' then you could cast to the generated interface to be like this:

    var currentPage = Umbraco.AssignedContentItem as ICommonPageProperties;
    var _title = currentPage!=null && !string.IsNullOrWhiteSpace(currentPage.Title) ? currentPage.Title : currentPage.Name;
    

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft