Copied to clipboard

Flag this post as spam?

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


  • Ault Nathanielsz 22 posts 106 karma points
    Jun 15, 2018 @ 09:54
    Ault Nathanielsz
    0

    Models binding and doctypes (NewB)

    Hi All,

    I am using the AppData version of ModelsBuilder in V7.10.4

    I have two doctypes: webpage and globalsettings.

    Using the webpage doctype I have a home page with several children - pretty much what you'd expect.

    I also have a single globalsettings node at the root. One of its fields is a multinode-tree-picker called "topNav" which I have used to select the pages that will appear in the top navigation.

    I use the following declaration at the beginning of my master view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.WebPage>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    

    This works fine, and allows me to get details from the current page thusly:

    Model.Content.Description
    

    I have added a partial to the view. I assume that to access the data in Globalsettings, I need to inherit from that model.

     inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.GlobalSettings>
    

    However, when the partial is added to the view...

               @Html.Partial("header/headerTopNav")
    

    I get the following error:

    Cannot bind source content type Umbraco.Web.PublishedContentModels.WebPage to model content type Umbraco.Web.PublishedContentModels.GlobalSettings.

    Any thoughts on where I am going wrong?

    Thanks in advance.

  • Nik 745 posts 2928 karma points MVP c-trib
    Jun 15, 2018 @ 10:01
    Nik
    100

    Hi Ault,

    The problem you are having is how you are calling your partial view.

    By using the code:

    @Html.Partial("header/headerTopNav") 
    

    you are, in effect, passing in the current page to the partial view which is why you are getting the binding error.

    What you need to do is retrieve your global settings node by doing something like this (Please note this is sudo code as I can't recall the exact code you'll need):

    var siteSettings= Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.ContentType.Alias.Equals("globalSettings")).OfType<GlobalSettings>(); 
    

    Then you can call the partial with the following change:

    @Html.Partial("header/headerTopName", siteSettings)
    

    This will pass in your siteSettings variable to the partial.

    Hope that helps :-)

    Nik

  • Ault Nathanielsz 22 posts 106 karma points
    Jun 15, 2018 @ 17:18
    Ault Nathanielsz
    1

    This all makes sense. And your sudo code seemed to be spot on.

    Worked a treat.

  • Nik 745 posts 2928 karma points MVP c-trib
    Jun 15, 2018 @ 18:09
    Nik
    0

    Excellent, pleased to hear it :-) Congrats on getting it working :-)

  • Ault Nathanielsz 22 posts 106 karma points
    27 days ago
    Ault Nathanielsz
    0

    Nik, if it's not too cheeky to ask, I have a follow-on to this question. Not sure if you saw it, but I'd appreciate any insight you may have.

    Thanks, Ault

    https://our.umbraco.org/forum/templates-partial-views-and-macros/92520-nav-from-multinode-tree-picker-ancestor-or-self

  • Nik 745 posts 2928 karma points MVP c-trib
    27 days ago
    Nik
    0

    No worries :-) I've dropped a reply on the thread for you. Hope it helps :-)

Please Sign in or register to post replies

Write your reply to:

Draft