Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Oct 10, 2014 @ 01:42
    Robin Nicholl
    0

    Pass child page url to razor macro with jquery

    Hi

    I have a Razor foreach loop writing something like:

    <li><a href="/page1">Page 1</a></li>
    <li><a href="/page2">Page 2</a></li>
    etc...
    

    I have a jQuery function capturing the click and want to load that page's data using a macro (via a template) that will format and display the contents of the relevant page.

    $('li a').on('click', function(e) {
        e.preventDefault();
        var pageUrl = $(this).attr('href');
        $('#target').load('/myTemplate', pageUrl);
    });
    

    In the "myTemplate" template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    @Umbraco.RenderMacro("MyMacro")
    

    and in the "MyMacro" macro:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <h3>@Model.pageTitle</h3>
    

    This obviously doesn't work at all!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 10, 2014 @ 08:17
    Dennis Aaen
    0

    Hi Robin,

    The reason why you canĀ“t get it to work is because that you are mixing MVC, with the old DynamicNode razor. You need to create a partial view or a partial view macro.

    http://our.umbraco.org/documentation/reference/templating/Mvc/views

    If you are choosing to use a partial view your top of the file should look like this:

    @inherits UmbracoTemplatePage

    In the partial view you can choose to use dynamic razor or strongly typed razor if you are using the dynamic razor it should look like this:

    <h3>@CurrentPage.pageTitle</h3>

    And the strongly typed version looks like this:

    @Model.Content.GetPropertyValue<string>("pageTitle")

    If you are choosing to use a parital view macro, you can again choose between dynamic razor and strongly typed razor, so this code is the same, but in parital view macro you need to inhert from this.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    Here are some documentation on how to rewrite your old DynamicNode Razor to MVC razor in the bottom of the page. http://our.umbraco.org/documentation/Reference/Templating/Macros/Partial-View-Macros/

    If you should create a parital view you will find the folder under the settings section, and if you go with the parital view macro, you will need to go to the developer section.

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft