Copied to clipboard

Flag this post as spam?

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


  • Lesley 107 posts 349 karma points
    Jun 09, 2017 @ 04:29
    Lesley
    0

    Cannot render a macro when UmbracoContext.PageId is null.

    I have a page with several navigation buttons. When a user clicks a button, a partial view is loaded into the page using a Surface Controller called via ajax.

    That part is working fine, however, I now need to render a partial macro within the partial view and I'm getting an error "Cannot render a macro when UmbracoContext.PageId is null".

    I presume this is because the partial view is being loaded via ajax.

    Does anyone know of a solution? Or is there a better way to load the partial view so that it retains the current UmbracoContext complete with pageId?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 09, 2017 @ 07:10
    Dan Diplo
    0

    Not sure how to get around your issue, but there is another way of achieving the same result (that's probably even easier).

    Create a new template that renders the content you want to load via AJAX - it should just return just the HTML you want to show (so no master). Render the content in your view using razor, as you would normally.

    Then you can load this template using the "alternative template" syntax Umbraco has, explained here: http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/templating/alt-template/documentation

    So if your new template was called AltTemplate and your existing page URL was http://www.example.com/articles/news/ you could then call http://www.example.com/articles/news/alttemplate/ to load your content.

    Now all you need to do is call the URL of your alternative template via an AJAX call and return the HTML and load it into a container div. eg. use http://api.jquery.com/load/ to load the template via AJAX into the page you wish the content to appear on.

Please Sign in or register to post replies

Write your reply to:

Draft