Copied to clipboard

Flag this post as spam?

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


  • Anders Brohäll 295 posts 561 karma points c-trib
    May 28, 2014 @ 10:45
    Anders Brohäll
    0

    Render PartialView (UmbracoTemplatePage) from Macro (PartialViewMacroPage)?

    Hi,

    Im trying to render a partial view from inside a macro.

    The macro is registered in the back-office, and has one parameter. The macro is supposed to be available from RTE's.

    The macro is based on a partial view, located in ~/Views/MacroPartials, that inherits Umbraco.Web.Macros.PartialViewMacroPage. Tha macro works, and the parameter gets populated as it's supposed.

    In the macros partial, i want to render a partial that i'm already using on the site - from various templates. The Partial inherits Umbraco.Web.Mvc.UmbracoTemplatePage and is rendered with @RenderPage() with three arguments on top of the path and model. However, when i'm including the Partial with RenderPage, i get an error message telling me:

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'.

    I guess that's reasonable, but still, i need to render the Partial from the Macro. How would you approach the problem? Can i change the inheritance in the Partial to make it play nice? What would that be?

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    May 28, 2014 @ 11:08
    Jeavon Leopold
    101

    Hi Anders,

    I would have your Partial View expect a model of IPublishedContent instead of RenderModel, then it can be easily used from both a view and a Partial View Macro.

    e.g.

    Partial View:

    @inherits UmbracoViewPage<IPublishedContent>
    @{
        var myHomePage = Model.AncestorOrSelf(1);
    }
    

    Then it can be called from either View or Macro:

    @Html.Partial("MyPartial", Model.Content)
    

    Jeavon

  • Anders Brohäll 295 posts 561 karma points c-trib
    May 28, 2014 @ 12:45
    Anders Brohäll
    0

    Got it working passing the params with a ViewDataDictionary.

    For future reference:

    var viewDataDictionary = new ViewDataDictionary
    { 
       {"items", items}, 
       {"count", count}, 
       {"heading", heading}
    };
    
    @Html.Partial("~/Views/Partials/FeedItems.cshtml", Model.Content, viewDataDictionary)
    

    And accessing the params in the partial as follows:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    @{
       var items = (IEnumerable<IPublishedContent>)ViewData["items"];
       var count = (int)ViewData["count"];
       var heading = (string)ViewData["heading"];
    }
    

    ... works from both templates and macro partials.

    Thank you for your help!

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    May 28, 2014 @ 13:15
    Jeavon Leopold
    0

    Perfect!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies