Copied to clipboard

Flag this post as spam?

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


  • Ben 19 posts 109 karma points
    Jan 24, 2019 @ 14:30
    Ben
    0

    Load content created in one doc type into multiple locations

    Hi All,

    Been struggling with a problem for a while now, sure its possibly but thing im going about it the wrong way. Probably due to my lack of "developer" knowledge.

    Ive created a page which lists events (seperate doctype)

    Events Page > Events

    Ive got a partial view i want to use on every page on the website which lists a small selection of "featured events". This following works great on the same page the model is being used (pageEvents) but cant use it anywhere else:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.PageEvents>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
                    @{
                        var typedMultiNodeTreePicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("featuredEventsList");
                        foreach (var item in typedMultiNodeTreePicker)
                        {
                             <span class="event-title">@item.Name</span>
                        }
                    }
    

    I get the error:

    Cannot bind source content type Umbraco.Web.PublishedContentModels.PageEvent to model content type Umbraco.Web.PublishedContentModels.PageEvents.

    So how do i go about using it elsewhere?

  • Kerri Mallinson 113 posts 497 karma points
    Jan 24, 2019 @ 16:08
    Kerri Mallinson
    0

    Hi Ben,

    Do you want the same featured events to appear on every page (is the MNTP on the events doc type) or are you wanting different events to be featured on each page (with a MNTP on every doc type)?

    Kerri

  • Ben 19 posts 109 karma points
    Jan 24, 2019 @ 16:16
    Ben
    0

    The same featured events will appear on every page.

    So the featured events will be only entered once using the MNTP in one place and then the hope was id use the same partial to show that on every page.

    Hope that answers the question.

  • Matthew Wise 195 posts 825 karma points c-trib
    Jan 24, 2019 @ 17:10
    Matthew Wise
    0

    Hi Ben,

    I would instead of getting your featured events in the partial I would suggest passing them in. I also notice you have models builder enabled so Ill write it using that :)

    Edit - The reason for the error is the Page you are on is no a PageEvents but I assume a PageEvent

    End Edit

    Events Listing Page:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.PageEvents>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
    @{
      if(Model.FeatureEventsList != null){
        @Html.Partial("Featured.cshtml", Model.FeaturedEventsList)
      }
    }
    

    Event Page:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.PageEvent>
        @using ContentModels = Umbraco.Web.PublishedContentModels;
    
        @{
    var listingPage = Model.Parent.OfType<PageEvents>();
          if( listingPage != null && listingPage.FeatureEventsList){
            @Html.Partial("Featured.cshtml", listingPage .FeaturedEventsList)
          }
        }
    

    Featured Parital View:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<IEnumerable<IPublishedConent>>
    @foreach (var item in Model)
              {
                  <span class="event-title">@item.Name</span>
              }
    

    Hope this helps

    Matt

  • Ben 19 posts 109 karma points
    Jan 24, 2019 @ 18:47
    Ben
    0

    Thanks for the reply, cant get this to work. Probably because of my lack of programming knowledge:

    Before the changes above this is what I have:

    PageEvent (contains details about the event and is a page, children of PageEvents) PageEvents (lists all of its children using the below code)

      @foreach (ContentModels.PageEvent pageEvent in Model.Content.Children)
            {
                <div class="event-link">
                    <div class="event-img-container">
                        <img src="@pageEvent.EventBanner.Url" />
                    </div>
                    <span class="event-list-date">@pageEvent.EventDate.ToString("dd MMM yyyy")</span>
                    <span class="event-list-title">@pageEvent.EventTitle</span>
                    <a class="event-list-link" href="@pageEvent.Url">More information</a>
                </div>
            }
    

    Then on other pages, for example PageHome and PageInfo ive tried to use my partial @Html.Partial("_events")

    which had in it the code at the start, but like you say i cant use that as im not on a PageEvents.

    Not sure if that information changes anything.

  • Matthew Wise 195 posts 825 karma points c-trib
    Jan 24, 2019 @ 21:36
    Matthew Wise
    0

    My example above will only work on PageEvents and its children.

    If you want to use the partial else where you will need to find the PageEvents page instead of using Model.Parent as in my example. There are a few ways you can do this but for now I recommend the UmbracoHelper and TypedContentAtXPath function

    @Umbraco.TypedContentAtXPath("//PageEvents")
    

    "Queries the XML Cache for Content matching a given XPath query and returns the first match as an IPublishedContent object."

    So on any page just replace

    var listingPage = Model.Parent.OfType<PageEvents>();
    

    with

    var listingPage = Umbraco.TypedContentAtXPath("//PageEvents").OfType<PageEvents>();
    

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft