Copied to clipboard

Flag this post as spam?

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

  • Jesper Ordrup 982 posts 1428 karma points MVP
    Mar 04, 2016 @ 22:45
    Jesper Ordrup

    Html.Partial in macroscript?

    Hi all,

    We have some macroscripts that runs and we cant change them to regular partials as they are part of a package.

    Is it possible in some way to use Html.Partial from a macroscript? It doesnt seem to be available.

    I want to render som partials and use the output in the macroscript.


  • Marc Goodson 1176 posts 7869 karma points MVP 5x c-trib
    Mar 06, 2016 @ 20:28
    Marc Goodson

    Hi Jesper

    Do you mean inside a Macro Partial View ? - Render a separate partial using Html.Partial or Html.RenderPartial etc ?

    if so then yes you can.

    In MVC when you use Html.Partial("viewname") the model that is passed to the partial view is the 'current model' of the view you are calling it from.

    Since you are inside a Macro Partial the current model is of type PartialViewMacroPage, so unless your Partial view is also based on this model you will get an error.

    The trick is to make the partial you want to render, inherit from IPublishedContent eg change the @inherits statement at the top of the partial (not the macro partial) to be:

    @inherits UmbracoViewPage

    Model.GetPropertyValue("aliasName") rather than Model.Content.GetPropertyValue("aliasName") etc/

    with this change made then inside the Macro you can render the partial view by passing the IPublishedContent element of the PartialViewMacroPage class like so:

    @{Html.RenderPartial("MyLovelyPartial", Model.Content);}



    if that makes sense ?



Please Sign in or register to post replies

Write your reply to: